判断单选框是否被选中
一、设置选中方法
代码如下:
$("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()); // 选中框中的值
});
我自己实际用到的:我使用到的是prop来获取,attr和prop是有区别的.
$("#checkAll").click(function(){
//通过单选框的属性checked的值为true还是false来进行判断
//if($("#checkAll").prop("checked")){ //第一种获取方式
if(this.checked){ //第二种获取方式
alert("选中单选框");
}else{
alert("取消选中");
}
});
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)