Coordination between CHECKBOX, JS, and PHP

Source: Internet
Author: User
Use checkbox in html as follows: struct [] valuedatastruct Data Structure inputtypecheckboxidmycouse2namemycouse [] valuec ++ C ++ inputtypecheckboxidmycouse3namemycouse [] valuec # C # inputt

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 t

Use checkbox in html as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.