简体   繁体   English

在JavaScript中循环遍历复选框不起作用

[英]Looping through check-box in JavaScript not working

I'm currently building an order form but it seems that the method I am testing to loop through the check-boxes to see if a value is ticked or not isn't working. 我目前正在建立一个订单,但似乎我正在测试的方法遍历复选框以查看是否勾选了一个值,这种方法不起作用。 What am I doing wrong? 我究竟做错了什么? In logic it seems all right - I would appreciate any help. 从逻辑上讲似乎还不错-我将不胜感激。 Thanks in advance :-) 提前致谢 :-)

<script>
function order()
{
for(i=1;i<=7;i++)
{   
alert(document.orderForm.order[i].checked);
}
}
</script>
    <form name='orderForm'>
      <p>Basic choices:</p>
      <p>
        <input type='checkbox' id='order1' name='order1' value='90' />
        <label for="1">&nbsp;Nano 1GB (&pound;90)</label>
        <br>
        <input type='checkbox' id='order2' name='order2' value='155' />
        <label for="2">&nbsp;Nano 4GB (&pound;155)</label>
        <br>
        <input type='checkbox' id='order3' name='order3' value='200' />
        <label for="3">&nbsp;Video 30GB (&pound;200)</label>
        <br>
        <input type='checkbox' id='order4' name='order4' value='275' />
        <label for="4">&nbsp;Video 60GB (&pound;275)</label>
      </p>
      <p>Options:</p>
      <p>
        <input type='checkbox' id='order5' name='order5' value='90' />
        <label for="5">&nbsp;Engraving (&pound;10)</label>
        <br>
        <input type='checkbox' id='order6' name='order6' value='15' />
        <label for="6">&nbsp;Carrying case (&pound;15)</label>
        <br>
        <input type='checkbox' id='order7' name='order7' value='18' />
        <label for="7">&nbsp;Car power adapter (&pound;18)</label>
      </p>
      <p>
        <input type="button" onClick="order();">
      </p>
      <p>Order Total</p>
      <p>
        <input type="text" name="orderTotal" id="orderTotal">
      </p>
      <p>VAT</p>
      <p>
        <input type="text" name="vat" id="vat">
      </p>
      <p>Order Total (+VAT)</p>
      <p>
        <input type="text" name="orderTotal_vat" id="orderTotal_vat">
      </p>
    </form>

Your current function implementation doesnt seem to be right.. well with the solution that you have here is how your funnction should look like: 您当前的函数实现似乎不正确。.与这里的解决方案一样,您的函数应如下所示:

<script>
function order()
{
for(i=1;i<=7;i++)
{   
alert(document.getElementById("order"+i).checked);
}
}
</script>

or you can fix your names and then get it work. 或者您可以改正名称,然后使其生效。 The array that you are trying to work with is constructed of a name that is shared across the checkboxes. 您要使用的数组由在复选框之间共享的名称构成。

<script>
function order()
{
with(document.orderForm)
    {
for(var i = 0; i < orderName.length; i++){
alert(orderName[i].checked);
}
    }
    }


</script>
    <form name='orderForm'>
      <p>Basic choices:</p>
      <p>
        <input type='checkbox' id='order1' name='orderName' value='90' />
        <label for="1">&nbsp;Nano 1GB (&pound;90)</label>
        <br>
        <input type='checkbox' id='order2' name='orderName' value='155' />
        <label for="2">&nbsp;Nano 4GB (&pound;155)</label>
        <br>
        <input type='checkbox' id='order3' name='orderName' value='200' />
        <label for="3">&nbsp;Video 30GB (&pound;200)</label>
        <br>
        <input type='checkbox' id='order4' name='orderName' value='275' />
        <label for="4">&nbsp;Video 60GB (&pound;275)</label>
      </p>
      <p>Options:</p>
      <p>
        <input type='checkbox' id='order5' name='orderName' value='90' />
        <label for="5">&nbsp;Engraving (&pound;10)</label>
        <br>
        <input type='checkbox' id='order6' name='orderName' value='15' />
        <label for="6">&nbsp;Carrying case (&pound;15)</label>
        <br>
        <input type='checkbox' id='order7' name='orderName' value='18' />
        <label for="7">&nbsp;Car power adapter (&pound;18)</label>
      </p>
      <p>
        <input type="button" onClick="order();">
      </p>
      <p>Order Total</p>
      <p>
        <input type="text" name="orderTotal" id="orderTotal">
      </p>
      <p>VAT</p>
      <p>
        <input type="text" name="vat" id="vat">
      </p>
      <p>Order Total (+VAT)</p>
      <p>
        <input type="text" name="orderTotal_vat" id="orderTotal_vat">
      </p>
    </form>

Fiddle: http://jsfiddle.net/P7CKG/ 小提琴: http//jsfiddle.net/P7CKG/

Without using with if you think that is confusing you: 如果不认为with会使您感到困惑,请不要使用:

<script>
function order()
{

for(var i = 0; i < document.orderForm.orderName.length; i++){
alert(document.orderForm.orderName[i].checked);
}
}  
</script>

document.orderForm.order is not an array. document.orderForm.order不是数组。 In fact, it doesn't exist. 实际上,它不存在。 You need to build your id like this: 您需要像这样构建自己的ID:

function order() {
    for(i=1;i<=7;i++) {   
        alert(document.orderForm["order" + i].checked);
    }
}

Here's a working fiddle . 是工作中的小提琴

Your function is failing because the form elements are not named order[1] , order[2] , etc: they are named order1 , order2 . 你的函数失败,因为表单元素没有被命名order[1] order[2]等等:它们被命名为order1order2 Thus, the code document.orderForm.order[i] references nothing. 因此,代码document.orderForm.order[i]没有引用任何内容。

Also, you should avoid accessing the form through the document.formName mechanism - get the element by ID instead. 另外,您应该避免通过document.formName机制访问表单-而是通过ID获取元素。 Likewise, rather than form.elementName , use form.elements[elementName] instead. 同样地,而不是form.elementName ,使用form.elements[elementName]代替。 These are, in my experience, more consistent between user agents. 以我的经验,用户代理之间的一致性更高。

function order () {
    var frm = document.getElementById('orderForm');
    for(i=1;i<=7;i++) {   
      alert(frm.elements['order'+i].checked);
    }
    return false;
}
function order(){
    var arrChks = document.orderForm.getElementsByTagName('input');
    for(i=0;i<arrChks.length;i++){
        if(arrChks[i].type=='checkbox'){
            alert(arrChks[i].checked);
        }
    }
}

but i reccomend you to use jquery or another javascript framework! 但我建议您使用jquery或其他javascript框架!

Complete example! 完整的例子! http://jsbin.com/amesek/ http://jsbin.com/amesek/

Zolved! Zolved!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM