How to obtain the radio button value on a web page

Source: Internet
Author: User
If a set of radio buttons are used in the form (Form name form1) on the web page, the name is button1.
To obtain the value of the selected button from JS, follow these steps:
<Script language = JavaScript>
Function getradiovalue ()
{
VaR STR;
For (I = 0; I <document. form1.button1. length; I ++)
{
If (document. form1.button1 [I]. Checked)
STR = Document. form1.button1 [I]. value;








}
}
</SCRIPT>
Here, the radio name button1 is equivalent to an array name.










Related examples:
<Input type = "radio" name = "radiobuttn" value = "A" Checked> one <br> <input type = "radio" name = "radiobuttn" value = "B"> two <br> <input type = "radio" name = "radiobuttn" value = "C"> three <br> <input type = "radio" name = "radiobuttn" Value = "D"> four all four radio button objects have the same name: radiobuttn,
HTML and JavaScript know that they are objects in the same number of groups. Second,
The value of each option is the alias of the option, not the text displayed (for example, "one" or "two ).

To obtain the value of the selected option, you must first know which option is selected, and then in the alert statement,
Use the value of the selected option to correctly reference the value of the current option. That is to say,
If the first option is selected, you should use radiobuttn [0]. value. If the second option is selected,
You should take radiobuttn [1]. value, and so on.

In JavaScript, some types of fields have the selectedindex attribute,
The value of the selected array. However, the single-choice button is not so lucky,
Similarly, the check box does not escape bad luck. To get the value of the selected radio button,
You must search for the "checked" attribute in the radiobuttn array element.

Below is the buttonCode:

VaR Doc = Document. Forms [0];
For (I = 0; I <Doc. radiobuttn. length; I ++)
{
If (Doc. radiobuttn [I]. Checked)
{
Alert (Doc. radiobuttn [I]. value );
Break;
}
}
RelatedArticleConnection of: http://www-900.ibm.com/developerWorks/cn/lotus/lo-jsobj/index.shtml

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.