doutiaosu2310 2015-12-12 15:08
浏览 547
已采纳

使用Javascript时出现“[object Object]”错误

I am trying to add an item to my cart by clicking an 'addtocart' button that submits the required information to a table in the database which holds the items being added to the cart. The issue I am dealing with occurs with the name of the item, in which I am getting an error saying [object Object] rather than the name of the item. I'm not sure why it's doing this since the name is able to be printed to the screen with the names of the items, but it isn't able to store it into a variable to be stored into the database.

<form method="post" action="store.php">                
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

        <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><?= $row3[1] ?></h4>
                    <input type="hidden" id="hiddenName" name="hiddenName" value=""/>
                </div>
            <div class="modal-body text-right">
                <img id="image" src="<?= $row3[4] ?>" alt="<?= $row3[1] ?>" type="image" width: "200px;">
                <input type="hidden" id="hiddenImage" name="hiddenImage" value=""/>
                <p id="description"><?= $row3[2] ?></p>
                <input type="hidden" id="hiddenDesc" name="hiddenDesc" value=""/>
                <h3 id="price"><?= $row3[5] ?></h3>
                <input type="hidden" id="hiddenPrice" name="hiddenPrice" value=""/>
                <input id="quantity" name="quantity" class="quantity" type="text" value="1" name="quantity">
            </div>
            <div class="modal-footer">
                <input type="submit" id="addtocart" name="addtocart" class="btn btn-default"  value="Add to Cart"/>
            </div>
        </div>
    </div>
</div>
</form>

<script>
    $('#myModal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget); // Button that triggered the modal
        var name = button.data('item'); // Extract info from data-* attributes
        var description = button.data('desc');
        var price = button.data('price');
        var image = button.data('img');

        var modal = $(this);
        var title = modal.find('.modal-title').text(name);
        document.getElementById('image').src= image;
        document.getElementById('description').innerHTML = description;
        document.getElementById('price').innerHTML = price;

        document.getElementById('hiddenName').value = title;
        document.getElementById('hiddenPrice').value = price;
        document.getElementById('hiddenDesc').value = description;
        document.getElementById('hiddenImage').value = image;
    })
</script>
  • 写回答

1条回答 默认 最新

  • dou70260 2015-12-12 15:14
    关注

    Beacuse where you are setting the title

    var title = modal.find('.modal-title').text(name);  
    

    The variable holds a reference to the .modal-title element and you set the value to the element, not the text of the element.

    document.getElementById('hiddenName').value = title;
    

    so you should be using name, not title

    document.getElementById('hiddenName').value = name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮