jquery判断单选按钮radio是否选中的方法

 

JQuery控制radio选中和不选中方法总结

1、设置选中方法
javascript

复制代码代码以下:

$("input[name='名字']").get(0).checked=true; 
$("input[name='名字']").attr('checked','true');
$("input[name='名字']:eq(0)").attr("checked",'checked'); 
$("input[name='radio_name'][checked]").val();  //获取被选中Radio的Value值

 

2、设置选中和不选中示例
html

复制代码代码以下:

<input type="radio" value="0" name="jizai" id="0"/>否
<input type="radio" value="1" name="jizai"  id="1"/>是
#jquery中,radio的选中与否是这么设置的。
$("#rdo1").attr("checked","checked");
$("#rdo1").removeAttr("checked");

 

经过namejava

复制代码代码以下:

$("input:radio[name="analyfsftype"]").eq(0).attr("checked",'checked');
$("input:radio[name="analyshowtype"]").attr("checked",false);


经过idjquery

复制代码代码以下:

$("#tradeType0").attr("checked","checked");
$("#tradeType1").attr("checked",false);

 

3、另外一种设置选中方法dom

 

复制代码代码以下:

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


您的性别:  this

复制代码代码以下:

<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>/>女

 

4、根据值设置radio选中spa

 

复制代码代码以下:

$("input[name='radio_name'][value='要选中Radio的Value值']").attr("checked",true);  //根据Value值设置Radio为选中状态

 

5、使用prop方法操做示例.net

 

复制代码代码以下:

$('input').removeAttr('checked'); 
$($('#'+id+'input').eq(0)).prop('checked',false);
$($('#'+id+' input').eq(0)).prop('checked',true);

 


 

JQuery radio(单选按钮)操做方法汇总

随着Jquery的做用愈来愈大,使用的朋友也愈来愈多。在Web中,因为CheckBox、Radiobutton 、DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的操做问题。因为Jquery的版本更新很快,代码的写法也改变了许多,如下Jquery代码适query1.4版本以上。code

1.获取选中值,三种方法均可以:
htm

复制代码代码以下:

$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();


2.设置第一个Radio为选中值:

复制代码代码以下:

    $('input:radio:first').attr('checked', 'checked');


或者

复制代码代码以下:

$('input:radio:first').attr('checked', 'true');


注:attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)

 

3.设置最后一个Radio为选中值:

复制代码代码以下:

$('input:radio:last').attr('checked', 'checked');


或者

复制代码代码以下:

$('input:radio:last').attr('checked', 'true');


4.根据索引值设置任意一个radio为选中值:

复制代码代码以下:

$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....


或者

复制代码代码以下:

$('input:radio').slice(1,2).attr('checked', 'true');


5.根据Value值设置Radio为选中值

复制代码代码以下:

$("input:radio[value='rd2']").attr('checked','true');


或者

复制代码代码以下:

$("input[value='rd2']").attr('checked','true');


6.删除Value值为rd2的Radio

复制代码代码以下:

$("input:radio[value='rd2']").remove();


7.删除第几个Radio

复制代码代码以下:

$("input:radio").eq(索引值).remove();索引值=0,1,2....


如删除第3个Radio:$("input:radio").eq(2).remove();

 

8.遍历Radio

复制代码代码以下:

$('input:radio').each(function(index,domEle){

 

     //写入代码

});

 


 

jquery判断单选按钮radio是否选中的方法

本文实例讲述了jquery判断单选按钮radio是否选中的方法。分享给你们供你们参考。具体以下:

html代码以下:

?
1
2
3
< 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. 加载页面的时候获取id

?
1
2
3
4
5
6
7
8
$( "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 为class
var s2 = $( "input[name='ra']:checked" ).val();

2.点击按钮的时候获取id

?
1
2
3
4
5
6
$( function (){
   $( "input[type='radio']" ).click( function (){
     var id= $( this ).attr( "id" );
     alert(id);
   });
});