Home >Web Front-end >JS Tutorial >JQuery control Radio selection method analysis_jquery

JQuery control Radio selection method analysis_jquery

WBOY
WBOYOriginal
2016-05-16 15:57:171261browse

The example in this article describes the JQuery control Radio selection method. Share it with everyone for your reference. The details are 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>

Summary:

If you want to implement radio selection in the HTML Radio control, such as the selection of men and women in this example, you need to add a name attribute to Radio with the same value; for example: name="sex".

Radio is selected by default:

jQuery(document).ready(function(){
  $("input[name=targetBlank]:eq(0)").attr("checked",'checked');
  $("input[name=status]:eq(0)").attr("checked",'checked');
});

Use jquery to get the value of radio. The most important thing is to master the use of jquery selector. In a form, we usually want to get the value of the selected radio item, so we need to add checked to filter. For example, there are the following Some radio items:

1.4bd7f1243937b39c877795dac8087712jquery gets the value of radio
2.80cdb9cd467e9eff7444b77c7162f6dajquery gets the value of the checkbox
3.0960d1f694fb708f039a61b076e6cae8jquery gets the value of select

To get the value of a certain radio, there are the following methods, just give the code directly:

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

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

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

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

It’s almost complete. What if we want to traverse all radios named testradio? The code is as follows

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

If you want to get the value of a specific radio, such as the value of the second radio, write like this

<script type="text/javascript">
$(document).ready(function(){
  $("input[@type=radio][name=sex][@value=1]").attr("checked",true);
}); 
</script>

Your gender:

<input type="radio" name="sex" value="1" <s:if test="user.sex==1">checked</s:if>/>男 
<input type="radio" name="sex" value="0" <s:if test="user.sex==0">checked</s:if>/>女 

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn