JavaScript Gets the selected radio value program code

Source: Internet
Author: User

JS Get Radio Value

The code is as follows Copy Code

<script language= "JavaScript" >
function Validateadd ()
{
var new=document.getelementsbyname ("bmfw_id");
var strnew;
for (Var i=0;i<new.length;i++)
{
if (New.item (i). Checked) {
Strnew=new.item (i). getattribute ("value");
Break
}else{
Continue
}
}
if (strnew== "no")
{
Alert ("ok! ");
return false;
}

}
</script>

jquery gets radio value simple

jquery gets radio value simple

To get a radio value there are several ways to give the code directly:

$ (' input[name= ' Testradio ']:checked '). Val ();

$ (' input:radio:checked '). Val ();

$ (' input[@name = ' testradio '][checked] ');

$ (' input[name= "Testradio"]). Filter (': Checked ');


Cases

The code is as follows Copy Code

<script type= "Text/javascript"
Src= "Https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script>
<script
Type= "Text/javascript" >
$ (function () {
$ (' #go '). Click (function () {
var radio = $ (' input[name= ' testradio '] '). Filter (': Checked ');
if (radio.length)
Alert (Radio.val ());
Else
Alert (' Please select a radio ');

});
$ (' #go2 '). Click (function () {
$ (' input[name= ' testradio '] '). each (function () {
alert (this.value);
});
})
$ (' #go3 '). Click (function () {
Alert ($ (' input[name= "Testradio"]:eq (1) '). Val ());
})
})
</script>

<body>
<input type= "Radio" name= "Testradio" value= "jquery gets radio value"
/>jquery get Radio value <BR/ > <input type= "Radio" name= "Testradio"
 value= "jquery Gets the value of the checkbox"/>jquery get the value of the checkbox <br/ >
<input type= "Radio" name= "Testradio" value= "jquery Gets the value of a select"
/>jquery gets the value of the Select <br/>
& Lt;button id= "Go" > Selected radio value </button>
<button id= "Go2" > Traverse all Radio values </button>
< Button id= "Go3" > take the value of the second radio </button>
</body>

Related Article

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.