Analysis of radio selection method of jquery control _jquery

Source: Internet
Author: User

This example describes the jquery control radio selection method. Share to everyone for your reference. Specifically as follows:

Method 1:

$ (function () { 
  $ ("#spanNan"). Click (function () { 
  $ ("#Radio1"). attr ("Checked", true); 
  $ ("#Radio2"). attr ("checked", false);
  $ ("#spanNv"). Click (function () {
  $ ("#Radio2"). attr ("Checked", true);
  $ ("#Radio1"). attr ("checked", false);
  })


Method 2: (Simple method)

$ (function () {
  $ ("#spanNan"). Click (function () {
  //$ ("#Radio1"). attr ("Checked", true);
  $ ("#Radio2"). attr ("checked", false);
  $ ("#Radio1"). Click ();
  };
  $ ("#spanNv"). Click (function () {
  //$ ("#Radio2"). attr ("Checked", true);
  $ ("#Radio1"). attr ("checked", false);
  $ ("#Radio2"). Click ();
  });


<input id= "Radio2" type= "Radio" name= "Sex"/> "<label" for= "
Radio2" > </label>

Summarize:

HTML radio controls to implement a single selection, such as the choice of men and women in this case, you need to add the Name property to the radio with the same value; Example: Name= "Sex".

Default Selected Radio:

JQuery (document). Ready (function () {
  $ ("input[name=targetblank]:eq (0)"). attr ("Checked", ' checked ');
  $ ("Input[name=status]:eq (0)"). attr ("Checked", ' checked ');
});

Using jquery to get the value of radio, and most importantly, the use of the jquery selector, in a form where we usually get the value of the selected radio item, we add checked to filter, such as the following radio items:

1.<input type= "Radio" name= "Testradio" value= "jquery gets radio value"/>jquery gets the value of radio
2.<input type= "Radio" name= "Testradio" value= "jquery Gets the value of the checkbox"/>jquery get the value of the checkbox
3.<input type= "Radio" name= "Testradio" value= "jquery Gets the value of a select"/>jquery gets the value of the Select

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 ');

It's pretty much all right, if we're going to traverse all the radio of name Testradio, the code is as follows

$ (' input[name= "Testradio"]). each (function () {alert (this.value);});

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

<script type= "Text/javascript" >
$ (document). Ready (function () {
  $ ("input[@type =radio][name=sex][@ Value=1] "). attr (" Checked ", true); 
</script>

Your sex:

<input type= "Radio" name= "Sex" value= "1" <s:if test= "user.sex==1" >checked</s:if>/> male 
< Input type= "Radio" name= "Sex" value= "0" <s:if test= "user.sex==0" >checked</s:if>/> 

I hope this article will help you with your jquery programming.

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.