A simple Ajax method for reading the value of a single selection box

Source: Internet
Author: User

In the AJAX submission processing vote, there is a question of extracting the ID value of a single box. After research, reference network, and finally achieved success. The following is an AJAX code fragment:
Get INPUT Element
var e=document.getelementsbytagname ("input");
Defines the initial value of a single marquee, which is valid when no option is selected, and uses it as a flag to select a single selection
var item_id= "";
for (Var i=0;i<e.length;i++) {
Radio is a radio box, name of a radio box, practical change according to the actual situation
if (e[i].type== "Radio" &&e[i].name== "item_id" &&e[i].checked)
When you select a radio box, get the value of this box
Item_id=e[i].value;
}
If you do not select a single selection, you are prompted for an error
if (item_id== "") {
Alert ("Please select the voting option!") ");
return false;
}else{
The following is the normal code of execution, slightly
}


The following is a better solution, tested successfully through:
var e=document.getelementsbyname ("item_id");
var item_id= "";
for (Var i=0;i<e.length;i++) {
if (e[i].checked)
Item_id=e[i].value;
}
if (item_id== "") {
Alert ("Please select the voting option!") ");
return false;
}else{
The following is the normal code of execution, slightly
}


Through the code can be seen, this code is targeted only to take name= "item_id" input elements, efficiency shows much more efficient, and no error.

A single check box effect is handled using the following code:
var f=document.getelementsbyname ("Isindex");
var isindex= "";
for (Var i=0;i<f.length;i++) {
if (f[i].checked)
Isindex=f[i].value;
}


Multiple check box effects, which is estimated to be an accumulation of values, the following code has not been tested:
Isindex=isindex+ "," +f[i].value;

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.