dongyan3616 2015-09-20 01:39
浏览 1185
已采纳

Javascript - 未捕获的TypeError:无法读取未定义的属性“值”

I am currently converting my friends website, as it's not "mobile friendly" by changing his current frontend into a bootstrap front end.

I've been working on a test site which is just a sub-domain on the same server.

This is the old website: http://bit.ly/1hurTNB

This is the new website: http://bit.ly/1hus0IV

But I've encountered a problem with the shopping cart page.

The shopping cart no longer recalculates its total when I press the recalculate button. It works fine on the old website. I dont know what I've changed to break it?

I've debugged the JavaScript using Chrome Dev Tools (F12) and line no 150 of order.php has this error:

Uncaught TypeError: Cannot read property 'value' of undefined 

And this is the line of offending code:

if (document.forms[1].elements["qty" + count].value == "" ||
    document.forms[1].elements["qty" + count].value == 0) {
        document.forms[1].elements["qty" + count].value = 0;
        showInCart = false;
}

I don't understand why I am getting this error? Googling around gives vague answers. Which is why I am here on StackOverflow.

BTW if you want to recreate my problem you'll need to:

  1. go to the homepage

  2. choose a product category from the grid of pictures e.g. "get costume ideas..."

  3. add an item to the basket.

  4. once the page posts to Order.php Change its Quantity.

  5. click the recalculate button.

Why is the Javascript no longer picking up the values from the quantity fields???

Any help is greatly appreciated.

Here is the function in its entirety:

<script type="text/javascript">
    function recalculateTotal(){        
        var lineTotal = 0;
        var subTotal = 0;
        var total = 0;
        var hiddenStuff = "";
        var count;
        var i;
        var orderURL="register.php?orderDetails=";

        items = new Array(<?=$itemList?>);                  

        for (i in items){
            var cNum = 0;
            var pNum = 0;
            var qNum = 0;

            count = items[i];

            cNum = (count * 4) + 1;

            var showInCart = true;                  

            // the next line is broken!
            if (document.forms[1].elements["qty" + count].value == "" || document.forms[1].elements["qty" + count].value == 0){
                document.forms[1].elements["qty" + count].value = 0;
                showInCart = false;
            }           

            lineTotal = document.forms[1].elements["qty" + count].value * document.forms[1].elements["price" + count].value;                
            document.getElementById("lTotal" + count).innerHTML = formatCurrency(lineTotal);
            subTotal += lineTotal;

            if (hiddenStuff == ""){
                hiddenStuff = hiddenStuff + showInCart + ":" + document.forms[1].elements["productId" + count].value + ':' + document.forms[1].elements["qty" + count].value;
            }else{
                hiddenStuff = hiddenStuff + ":" + showInCart + ":" + document.forms[1].elements["productId" + count].value + ':' + document.forms[1].elements["qty" + count].value;
            }

        }           

        document.getElementById("subTotal").innerHTML = formatCurrency(subTotal);
        for (var j in delivery_prices){
            if (subTotal >= delivery_prices[j].total_amount_start && subTotal <= delivery_prices[j].total_amount_end){
                document.getElementById('delivery').innerHTML = delivery_prices[j].delivery_price;
                total = subTotal + delivery_prices[j].delivery_price;
            }
        }
        document.getElementById("total").innerHTML = formatCurrency(total);
        document.forms[1].elements["orderDetails"].value = hiddenStuff;
        orderURL = orderURL + hiddenStuff;
        var myrequest = $.ajax({
                url: orderURL,
                type: "get",
            });
        myrequest.done(function (response){
            updateBasket();
        });

    }
</script>

Thanks very much.

展开全部

  • 写回答

4条回答 默认 最新

  • douhe4336 2015-09-20 01:59
    关注

    This error arises when the elements are not either declared or not loaded on the DOM. You can do these things to solve this problem :

    1.Wrap every thing inside a function and attach it to the window.onload. Like this :

    <script type = "text/javascript">
    function foo(){
    .....//Your JavaScript code here
    }
    
    window.onload = foo;
    </script>
    
    1. Check whether all the elements are present in the HTML page or not.

    2. I am not sure that this is the problem, but I think its worth mentioning. Change the line no.150 to :

    if (document.forms[0].elements["qty" + count].value == "" ||
        document.forms[0].elements["qty" + count].value == 0) {
        document.forms[0].elements["qty" + count].value = 0;
        showInCart = false;
    }
    

    In the above code snippet I've changed the occurrence of 1 with 0. Because JavaScript starts counting from 0 not 1. So if you want to access the content of first form then use this point otherwise ignore this point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 Mysql 一张表同时多人查询和插入怎么防止死锁
  • ¥20 centos6.7 安装libevent库.总是报错,如何解决?
  • ¥15 电脑买回,学校的有线网络总掉。
  • ¥20 关于普洛菲斯触摸屏与AB连接地址问题
  • ¥15 vue但是页面显示的数据为空为什么呀,明明在钩子函数中已经成功赋值(相关搜索:输出数据)
  • ¥15 syri可视化不显示插入缺失
  • ¥30 运行软件卡死查看系统日志分析不出来
  • ¥15 C语言代码改正特征选择算法设计,贝叶斯决策,,设计分类器,远程操作代码修正一下
  • ¥15 String 类valuve指向的问题
  • ¥15 在ros2的iron版本进行编译时遇到如下问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部