Use jquery to get the value of radio

Source: Internet
Author: User

Using jquery to get the value of Radio , the most important thing is to master the use of jquery selectors, in a form we usually want to get the value of the selected radio item, so add checked to filter, for example, there are some of the following radio items:

1. < input  type = "radio"  name = "testradio"  value = "jquery获取radio的值"  />jquery获取radio的值< br  /> 2. < input  type = "radio"  name = "testradio"  value = "jquery获取checkbox的值"  />jquery获取checkbox的值< br  /> 3. < input  type = "radio"  name = "testradio"  value = "jquery获取select的值"  />jquery获取select的值< br  />

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

1.$(‘input[name="testradio"]:checked‘).val();

2.$(‘input:radio:checked‘).val();

3.$(‘input[@name="testradio"][checked]‘);

4.

  1. $( ‘input[name="testradio"]‘ ).filter( ‘:checked‘ );

Pretty much, if we're going to go through all the radio name Testradio, here's the code.

 1. $( ‘input[name="testradio"]‘ ).each( function (){  2. alert( this .value);  3. });

If you want to take the value of a specific radio, such as the value of a second radio, write

 1. $( ‘input[name="testradio"]:eq(1)‘ ).val()

To deepen your impressions, run the following instance by modifying

"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 () {varRadio = $ ('input[name= "Testradio"]'). Filter (': Checked'); if(radio.length) alert (Radio.val ()); ElseAlert ('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>"Radio"Name="Testradio"Value="jquery Gets the value of radio"/>jquery gets the value of radio <br/> <input type="Radio"Name="Testradio"value="jquery Gets the value of the checkbox"/>jquery gets the value of the checkbox <br/> <input type="Radio"Name="Testradio"Value="jquery Gets the value of select"/>jquery gets the value of select <br/><button id="Go"> The value of the radio selected </button><button id="Go2"> traverse the values of all radio </button><button id="Go3"> Take the value of a second radio </button></body>

Use jquery to get the value of radio

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.