jquery Determines whether the radio button radio is selected

Source: Internet
Author: User

jquery Control Radio Check and uncheck Method summary

First, set the selected method

Copy CodeThe code is as follows:
$ ("Input[name= ' name ']"). Get (0). Checked=true;
$ ("Input[name= ' name ']"). attr (' checked ', ' true ');
$ ("Input[name= ' name ']:eq (0)"). attr ("Checked", ' checked ');
$ ("Input[name= ' Radio_name '][checked]"). Val (); Gets the value of the selected radio

Two, set the check and uncheck the example

Copy CodeThe code is as follows:
<input type= "Radio" value= "0" name= "Jizai" id= "0"/> No
<input type= "Radio" value= "1" name= "Jizai" id= "1"/> Yes
#jquery中, the selection of radio is so set.
$ ("#rdo1"). attr ("Checked", "checked");
$ ("#rdo1"). Removeattr ("checked");

By name

Copy CodeThe code is as follows:
$ ("input:radio[name=" Analyfsftype "]"). EQ (0). attr ("Checked", ' checked ');
$ ("input:radio[name=" Analyshowtype "]"). attr ("checked", false);


by ID

Copy CodeThe code is as follows:
$ ("#tradeType0"). attr ("Checked", "checked");
$ ("#tradeType1"). attr ("checked", false);

Third, another set the selection method

Copy CodeThe code is as follows:
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("input[@type =radio][name=sex][@value =1]"). attr ("Checked", true);
});
</script>


Your gender:

Copy CodeThe code is as follows:
<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>/> female

Four, according to the value setting radio selected

Copy CodeThe code is as follows:
$ ("Input[name= ' radio_name '][value= ' to select the value of Radio ')"). attr ("Checked", true); Set Radio to selected based on value values

V. Using the Prop method to manipulate the example

Copy CodeThe code is as follows:
$ (' input '). Removeattr (' checked ');
$ ($ (' # ' +id+ ' input '). EQ (0)). Prop (' checked ', false);
$ ($ (' # ' +id+ ' input '). EQ (0)). Prop (' checked ', true);

JQuery Radio (radio button) how-to Summary

As jquery has become more and more important, more and more friends are being used. In the web, because controls such as checkboxes, Radiobutton, DropDownList, and so on are used more frequently, they are related to the operation of these controls in jquery. Since the version of jquery is updated very quickly, the code has changed a lot, the following jquery code is suitable for query1.4 version above.

1. To get the selected value, three methods are available:

Copy CodeThe code is as follows:
$ (' input:radio:checked '). Val ();
$ ("input[type= ' Radio ']:checked"). Val ();
$ ("Input[name= ' rd ']:checked"). Val ();


2. Set the first radio to the selected value:

Copy CodeThe code is as follows:
$ (' Input:radio:first '). attr (' checked ', ' checked ');


Or

Copy CodeThe code is as follows:
$ (' Input:radio:first '). attr (' checked ', ' true ');


Note: attr ("Checked", ' checked ') = attr ("Checked", ' true ') = attr ("Checked", true)

3. Set the last radio as the selected value:

Copy CodeThe code is as follows:
$ (' Input:radio:last '). attr (' checked ', ' checked ');


Or

Copy CodeThe code is as follows:
$ (' Input:radio:last '). attr (' checked ', ' true ');


4. Set any one of the radio to the selected value according to the index value:

Copy CodeThe code is as follows:
$ (' Input:radio '). EQ (index value). attr (' checked ', ' true '); index value =0,1,2 ....


Or

Copy CodeThe code is as follows:
$ (' Input:radio '). Slice. attr (' checked ', ' true ');


5. Set Radio to the selected value based on value

Copy CodeThe code is as follows:
$ ("input:radio[value= ' Rd2 ')"). attr (' checked ', ' true ');


Or

Copy CodeThe code is as follows:
$ ("input[value= ' Rd2 ')"). attr (' checked ', ' true ');


6. Remove Radio with value Rd2

Copy CodeThe code is as follows:
$ ("input:radio[value= ' Rd2 ')". Remove ();


7. Delete the first few radio

Copy CodeThe code is as follows:
$ ("Input:radio"). EQ (index value). Remove (); index value =0,1,2 ....


such as removing the 3rd radio:$ ("Input:radio"). EQ (2). Remove ();

8. Traverse Radio

Copy CodeThe code is as follows:
$ (' Input:radio '). each (function (Index,domele) {

Write code

});

jquery Determines whether the radio button radio is selected

This example describes how jquery determines whether a radio button radio is selected. Share to everyone for your reference. Specific as follows:

The HTML code is as follows:

?
123 <input type="radio" id="d1" name="ra" value="a" checked="checked" /><input type="radio" id="d2" name="ra" value="b" /><input type="radio" id="d3" name="ra" value="c" />

1. Get ID when loading the page

?
12345678 $("input[type=‘radio‘]").each(function(){  var id= $(this).attr("id");  if($("#"+id).attr("checked")=="checked"){   var fs=$("#"+id).val();  }});var s1 = $(".aa:checked").val(); // .aa 为classvar s2 = $("input[name=‘ra‘]:checked").val();

2. Get the ID when you click on the button

?
123456 $(function(){  $("input[type=‘radio‘]").click(function(){    varid= $(this).attr("id");    alert(id);  });});

jquery Determines whether the radio button radio is selected

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.