radio(單選框)反覆選中與取消選中

作個記錄,以便須要拿取javascript

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

第一種dom

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

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

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;
}
}
}); ip


});
</script>input

相關文章
相關標籤/搜索