radio(单选框)反复选中与取消选中

做个记录,以便需要拿取

<script type="text/javascript">
$(function(){

第一种

$('input:radio').click(function(){
//alert(this.checked);
//

var domName = $(this).attr('name');

var $radio = $(this);
// if this was previously checked
if ($radio.data('waschecked') == true){
$radio.prop('checked', false);
$("input:radio[name='" + domName + "']").data('waschecked',false);
//$radio.data('waschecked', false);
} else {
$radio.prop('checked', true);
$("input:radio[name='" + domName + "']").data('waschecked',false);
$radio.data('waschecked', true);
}
});

第二种

var old = null; //用来保存原来的对象
$("input[name='sex']").each(function(){//循环绑定事件
if(this.checked){
old = this; //如果当前对象选中,保存该对象
}
this.onclick = function(){
if(this == old){//如果点击的对象原来是选中的,取消选中
this.checked = false;
old = null;
} else{
old = this;
}
}
});


});
</script>

原文地址:https://www.cnblogs.com/lzx2509254166/p/8874085.html