Javascript循环获取已选中复选框的名称

时间:2015-06-29 03:37:48

标签: javascript jquery checkbox

我有HTML显示通过循环检查的复选框:

<!DOCTYPE html>

<html>
    <head>
        <title></title>
        <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    </head>

    <body>
        <form>
            <input type="checkbox" name="bla_bla1" id="checkbox_1" onchange="getName();"/>
            <input type="checkbox" name="bla_bla2" id="checkbox_2" onchange="getName();"/>
            <input type="checkbox" name="bla_bla3" id="checkbox_3" onchange="getName();"/>
        </form>     
        <span id="checkboxConfirm_1"></span>
        <span id="checkboxConfirm_2"></span>
        <span id="checkboxConfirm_3"></span>

        <script>            
            function getName(){
                for(i = 1; i<4; i++){
                var a = "#checkbox_".concat(i.toString()) ;
                var b = "#checkboxConfirm_".concat(i.toString());               

                if ((a).is(":checked")) {
                    $(b).text($(a).attr("name"));
                } else {
                    $(b).text('');
                }
            }
            }
        </script>   
    </body>
</html>

但Javascript无效。请帮我解决问题。

1 个答案:

答案 0 :(得分:2)

不要为每个复选框使用onChange并且不需要使用for循环使用css的正则表达式选择器,它将解决您的问题。

请参阅此示例:http://jsfiddle.net/kevalbhatt18/gpdjoyxx/1/

Thread 5 name:  Dispatch queue: com.apple.root.default-qos
Thread 5 Crashed:
0   libicucore.A.dylib              0x00000001967516dc 0x1965c8000 + 1611484
1   libicucore.A.dylib              0x00000001967516d0 0x1965c8000 + 1611472
2   Foundation                      0x000000018638a3dc 0x186320000 + 435164
3   Foundation                      0x0000000186389c38 0x186320000 + 433208
4   PAYBACK                         0x00000001000b28bc 0x10009c000 + 92348
5   PAYBACK                         0x00000001000b2974 0x10009c000 + 92532
6   PAYBACK                         0x00000001000ea2d4 0x10009c000 + 320212
7   libdispatch.dylib               0x0000000197285990 0x197284000 + 6544
8   libdispatch.dylib               0x0000000197285950 0x197284000 + 6480
9   libdispatch.dylib               0x000000019729277c 0x197284000 + 59260
10  libdispatch.dylib               0x0000000197293c48 0x197284000 + 64584
11  libsystem_pthread.dylib         0x0000000197465228 0x197464000 + 4648
12  libsystem_pthread.dylib       

Thread 12 name:  WebCore: LocalStorage
Thread 12:
0   libsystem_kernel.dylib          0x00000001973cb078 0x1973b0000 + 110712
1   libsystem_pthread.dylib         0x0000000197466f28 0x197464000 + 12072
2   JavaScriptCore                  0x00000001869e97bc 0x1869d0000 + 104380
3   WebCore                         0x000000019466e32c 0x193a34000 + 12821292
4   WebCore                         0x0000000193be0ff8 0x193a34000 + 1757176
5   JavaScriptCore                  0x00000001869e4b9c 0x1869d0000 + 84892
6   libsystem_pthread.dylib         0x0000000197467dc4 0x197464000 + 15812
7   libsystem_pthread.dylib         0x0000000197467d20 0x197464000 + 15648
8   libsystem_pthread.dylib         0x0000000197464ef4 0x197464000 + 3828

Thread 5 crashed with ARM Thread State (64-bit):
    x0: 0x000000015d51db40   x1: 0x0000000000000000   x2: 0xffffffffffffffe0   x3: 0x0000000101cef8a0
    x4: 0x0000000101cef900   x5: 0x0000000196710450   x6: 0x0000000101cefce8   x7: 0x0000000000000000
    x8: 0x4000000000000000   x9: 0x0000000000000006  x10: 0x0000000000000000  x11: 0x0000000000000000
   x12: 0x0000000000000592  x13: 0x0000000000000000  x14: 0x0000000000000000  x15: 0x0000000000000000
   x16: 0x000000019745d580  x17: 0x00000001853fdd64  x18: 0x0000000000000000  x19: 0x0000000101cefc08
   x20: 0x0000000170266fc0  x21: 0x0000000101cefa08  x22: 0x0000000000000000  x23: 0x0000000101cef850
   x24: 0x0000000101cef9fc  x25: 0x0000000000000000  x26: 0x000000018a77ff43  x27: 0x000000018a72d172
   x28: 0x0000000170057eb0  fp: 0x0000000101cef910   lr: 0x00000001967516d4
    sp: 0x0000000101cef850   pc: 0x00000001967516dc cpsr: 0x60000000