Use checkbox in HTML as follows:
<Form name = "frm1">
<Input type = "checkbox" id = "mycouse1" name = "mycouse []" value = "datastruct"> Data Structure
<Input type = "checkbox" id = "mycouse2" name = "mycouse []" value = "C ++"> C ++
<Input type = "checkbox" id = "mycouse3" name = "mycouse []" value = "C #"> C #
<Input type = "checkbox" id = "mycouse4" name = "mycouse []" value = "php"> PHP
</Form>
Note that the "[]" character in the checkbox name is used as an array. in PHP, you can use the foreach method to obtain the selected value.
However, the document. frm1.mycouse []. length method cannot be used in Js. Only document. frm1.mycouse. length can be used. In this way, PHP can only take the last selected mycouse value and cannot obtain all the selected mycouse values.
Solution: Document. frm1 ["mycouse []"]. length. Think of the entire form as an array, and use checkbox [] as one of the elements.
Checkbox_mycouse = Document. frm1 ["mycouse []"];
For (VAR I = 0; I <= checkbox_mycouse.length-1; I ++)
{
If (checkbox_mycouse [I]. Checked)
Count ++;
}
Alert ('you' + Count + 'option is selected. ');
In fact, if name = "mycouse []" is name = "mycouse", it is very simple:
For (VAR I = 0; I <= Document. frm1.mycouse. Length-1; I ++)
{
If (document. frm1.mycouse [I]. Checked)
Count ++;
}
Author: Zhang Qing (mesh), microblog: http://t.qq.com/zhangking
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com
2012-8-22