What is wrong with this Javascript? shopping cart

后端 未结 3 1096
自闭症患者
自闭症患者 2021-01-29 13:47

There is something in this javascript or html which is is allowing the checkboxes to be ticked but for not even half a second. (I need the checks to stay there!) I also need the

相关标签:
3条回答
  • 2021-01-29 14:01

    Half of your if statements are missing parentheses, that's some basic wrongfulness.

    I don't know what and where should any of the variables be, but here is my best shot:

    function addItems(field) {
        var i;
        for (i = 0; i < field.length; i++) {
            if (field[i].checked === true) {
                if (computer[i] !== null) { 
                    selected[i] = computer[i];
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 14:08

    Your JS syntax is way off, this is what it should look like

    function addItems(field) {
        for (i = 0; i <= field.length-1; i++) 
        {
            if (field[i].checked == true)
            {
                if (computer[i]!=null) { 
                    selected[i] = computer[i];
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 14:20

    You are using i = 0 rather than var i = 0, which will introduce a global variable. This could be a problem if you're writing similar code elsewhere.

    Your if-statements are not statements at all. They look like pseudo-code. You're also comparing with = rather than ==, which will cause an assignment rather than a condition, even if you fix up your syntax.

    You are not properly indenting your code, which will make you much more prone to introduce new errors.

    These are the general issues I notice immediately. Of course, heaps of things could be wrong with this code. fields might not be an array, computer and selected might not match the size of fields, etc.

    If you have any specific problem, please describe that, and we may be able to address it.

    0 讨论(0)
提交回复
热议问题