但是我試起來好像都有點怪怪的~
例如是有看到有人分享的 selection 方法可以直接檢查 "checked" 這個屬性
但是我的測試好像沒有用:
1 2 3 |
$( "input[name='multi_choice'][@checked]" ).each( function (value, element) { console.log( "element: " + $(element).attr( "checked" )); }); |
後來查到 [2] 的寫法,就可以順利取道被選取的 checkbox 了。
HTML
1 2 3 4 5 |
<input type= "checkbox" name= "multi_choice" ></input> <input type= "checkbox" name= "multi_choice" ></input> <input type= "checkbox" name= "multi_choice" ></input> <input type= "checkbox" name= "multi_choice" ></input> <input type= "checkbox" name= "multi_choice" ></input> |
1 2 3 |
$( "input[name='multi_choice']:checked" ).each( function (value, element) { console.log( "element: " + $(element).attr( "checked" )); }); |
另外 jQuery selector 選出來的 Object 可以直接用 .length() 取得數目
因此可以利用 .length() 取得被選取的 checkbox 的數量。
1 |
$( "input[name='multi_choice']:checked" ).length(); |
參考資料:
1、jQuery selectors
2、jquery multiple checkboxes array
沒有留言:
張貼留言