doutiaosu2310 2015-12-12 07:08
浏览 550
已采纳

使用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 07: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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 Java AES 算法 加密采用24位向量报错如何处理?
  • ¥15 使用X11可以找到托盘句柄,监控到窗口点击事件但是如何在监听的同时获取托盘中应用的上下文菜单句柄
  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 求GCMS辅导数据分析