radio可取消操作,需要手动处理。网上的争论无非就是attr和prop的使用
亲测都有问题(比如多次选中取消不生效,有时需要点击两次才能选中等等),
关键点就是
.prop获取checked达不到预期
.attr设置checked达不到预期 得等
这里结合使用就没问题了(划重点:attr用来记录获取,prop用来设置)
html:
<label class="col-sm-2 control-label"><span style="color: red;margin-top:3px;"></span>选项</label>
<div class="col-sm-4">
<div class="checkbox-inline" style="margin-left:0px">
<input type="radio" id="xx1" name="xx" value="1" <c:if test="${xx.xx1 eq 'Y'}"> checked </c:if> style="font-size: 17px"/>选项1
</div>
<div class="checkbox-inline" style="margin-left:0px">
<input type="radio" id="xx2" name="xx" value="2" <c:if test="${xx.xx2 eq 'Y'}"> checked </c:if> style="font-size: 17px"/>选项2
</div>
</div>
js:
$(document).ready(function () {
$("input:radio[name=xx]").click(function(){
var domName = $(this).attr('name');//获取当前单选框控件name 属性值
var checkedState = $(this).attr('checked');//记录当前选中状态
$("input:radio[name=xx]").attr('checked',false);
$(this).prop('checked',false); //3.
if(checkedState == 'checked'){
$(this).prop('checked',false); //3.
$(this).attr('checked',false); //3.
}else{
$(this).prop('checked',true); //3.
$(this).attr('checked','checked'); //3.
}
});
});