Use javascript to obtain the checkbox check box to obtain the selected option, including javascriptcheckbox
There are countless instances for javascript to retrieve the checkbox check box. In the following example
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";} alert (message);/* obtain the selected option */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> sort: </label> <input id = 'aaaaa' type = 'checkbox' name = 'test2' value = '1'> <label for = 'aaaaa'> monthly sales </label> <input id = 'bbbbbb' type = 'checkbox' name = 'test2' value = '2'> <label for = 'bbbbb'> score </label> <input id = 'ccccccc' type = 'checkbox' name = 'test2' value = '3'> <label for = 'ccccc '> discount </label> <br style = 'clear: both '> <label> category: </label> <input id = 'ddddd 'type = 'checkbox' name = 'test2' value = '4'> <label for = 'ddddd'> Business Plan </label> <input id = 'eeeee' type = 'checkbox' name = 'test2' value = '5'> <label for = 'eeeee '> cold dish </label> <input id = 'fffff' type = 'checkbox' name = 'test2' value = '6'> </form> ",
How does javascript obtain and calculate the checkbox value?
Hello!
Write a simple example as follows:
Html page:
<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 = "sum" onclick = "Math_Click ()"/>
JS section:
<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>
The principle is to set the name values of all the checkboxes to the same, and then find out all the traversal operations to determine whether the checkbox is selected and then add them. Note that, other tags on the page may be affected if they have the same name value.
How to use Javascript to obtain values of other columns of the row selected in the checkbox
Add the table ID as "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); // print a statement such as abc 1 0 1. If none of the rows is selected, the statement is not printed.
}
}