JQuery控制radio選中和不選中方法總結
一、設置選中方法
$("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值
二、設置選中和不選中示例
<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");
通過name
$("input:radio[name="analyfsftype"]").eq(0).attr("checked",'checked');
$("input:radio[name="analyshowtype"]").attr("checked",false);
通過id
$("#tradeType0").attr("checked","checked");
$("#tradeType1").attr("checked",false);
三、另一種設置選中方法
<script type="text/javascript">
$(document).ready(function(){
$("input[@type=radio][name=sex][@value=1]").attr("checked",true);
});
</script>
您的性別:
<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>/>女
四、根據值設置radio選中
$("input[name='radio_name'][value='要選中Radio的Value值']").attr("checked",true); //根據Value值設置Radio為選中狀態
五、使用prop方法操作示例
$('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版本以上。
1.獲取選中值,三種方法都可以:
$('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);
});
});
|