javascript擷取checkbox複選框擷取選中的選項,javascriptcheckbox
有關javascript 擷取checkbox複選框的執行個體數不勝數,下面的這個樣本,純js實現的
var form = document.getElementById("form2");var field = form.elements["test2"];var option = Dining.getSelectedOption(form, field);var message = "";for (var i = 0, len = option.length; i < len; i++) {message += "Select id:" + option[i].id + "\nSelected name:" + option[i].name + "\nSelected value:" + option[i].value + "\n\n";}alert(message);/*擷取選中的選項*/getSelectedOption: function (selectform, selectionfield) {var result = [];var option = null;for (var i = 0; i < selectionfield.length; i++) {option = selectionfield[i];if (option.checked) {result.push(option);}}return result;}<form id='form2'><label>排序:</label><input id='aaaaa' type='checkbox' name='test2' value='1'><label for='aaaaa'>月銷量</label><input id='bbbbb' type='checkbox' name='test2' value='2'><label for='bbbbb'>評分</label><input id='ccccc' type='checkbox' name='test2' value='3'><label for='ccccc'>優惠</label> <br style='clear:both'><label>分類:</label><input id='ddddd' type='checkbox' name='test2' value='4'><label for='ddddd'>商務套餐</label><input id='eeeee' type='checkbox' name='test2' value='5'><label for='eeeee'>涼菜</label><input id='fffff' type='checkbox' name='test2' value='6'></form>",
javascript怎擷取checkbox值並且計算
你好!
寫個簡單的例子,如下:
html頁面部分:
<input type="checkbox" value="1" name="check"/>1
<input type="checkbox" value="2" name="check"/>2
<input type="checkbox" value="3" name="check"/>3
<input type="checkbox" value="4" name="check"/>4
<input type="checkbox" value="5" name="check"/>5
<input type="button" value="求和" onclick="Math_Click()"/>
JS部分:
<script type="text/javascript">
function Math_Click(){
var rusult=0;
var check_array=document.getElementsByName("check");
for(var i=0;i<check_array.length;i++)
{
if(check_array[i].checked==true)
{
rusult=parseInt(rusult)+parseInt(check_array[i].value);
}
}
alert(rusult);
}
</script>
原理是把所有的checkbox的name值設為一樣,然後查出所有的進行遍曆,判斷是否是選中狀態然後再相加,需注意的是,頁面其他標籤如果有name值相同的可能會有影響,這個自己把握了
用Javascript怎擷取 checkbox 所選中的行的 其他列的 值
給那table加個ID為“table”
var rows = document.getElementById("table").rows;
for(var i=0;i<rows.length;i++){
if(rows[i].cells[2].firstChild.checked||rows[i].cells[3].firstChild.checked||rows[i].cells[4].firstChild.checked){
var str = rows[i].cells[0].innerHTML+" "+rows[i].cells[1].innerHTML;
if(rows[i].cells[2].firstChild.checked)
str += " "+1;
else
str += " "+0;
if(rows[i].cells[3].firstChild.checked)
str += " "+1;
else
str += " "+0;
if(rows[i].cells[4].firstChild.checked)
str += " "+1;
else
str += " "+0;
alert(str);//列印出如:abc abc 1 0 1之類的語句,如果那行一個都沒有選則不列印
}
}