JQuery判斷radio單選框是否選中並獲取值的方法

在本篇文章裏小編給大家分享了關於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);

radio不能用“checked”相等來判斷,只用用true來判斷

代碼如下:

<script type="text/javascript">
$(function () {
$("input").click(function () {
if ($(this).attr("checked")) {
alert("選中了");
}
});
});
</script>

六、jQuery獲取循環中的選中單選按鈕radio的值

$('input:radio:checked').each(function(){
//var checkValue = $(this).val();
console.log($(this).val());  // 選中框中的值
});

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章