有關(guān)javascript 獲取checkbox復(fù)選框的實(shí)例數(shù)不勝數(shù),下面的這個示例,純js實(shí)現(xiàn)的
view sourceprint?
01 var form = document.getElementById("form2");
02
03 var field = form.elements["test2"];
04 var option = Dining.getSelectedOption(form, field);
05 var message = "";
06 for (var i = 0, len = option.length; i < len; i++) {
07 message += "Select id:" + option[i].id + "\nSelected name:" + option[i].name + "\nSelected value:" + option[i].value + "\n\n";
08 }
09 alert(message);
10 /*獲取選中的選項(xiàng)*/
11 getSelectedOption: function (selectform, selectionfield) {
12 var result = [];
13 var option = null;
14 for (var i = 0; i < selectionfield.length; i++) {
15 option = selectionfield[i];
16 if (option.checked) {
17 result.push(option);
18 }
19
20 }
21 return result;
22
23 }
24 <form id='form2'>
25
26 <label>排序:</label><input id='aaaaa' type='checkbox' name='test2' value='1'>
27
28 <label for='aaaaa'>月銷量</label><input id='bbbbb' type='checkbox' name='test2' value='2'>
29
30 <label for='bbbbb'>評分</label><input id='ccccc' type='checkbox' name='test2' value='3'>
31
32 <label for='ccccc'>優(yōu)惠</label> <br style='clear:both'><label>分類:</label><input id='ddddd' type='checkbox' name='test2' value='4'>
33
34 <label for='ddddd'>商務(wù)套餐</label><input id='eeeee' type='checkbox' name='test2' value='5'>
35
36 <label for='eeeee'>涼菜</label><input id='fffff' type='checkbox' name='test2' value='6'>
37
38 </form>",
更多信息請查看IT技術(shù)專欄