[英]How to uncheck a radio button?
我有一组单选按钮,我想在使用 jQuery 提交 AJAX 表单后取消选中它们。 我有以下功能:
function clearForm(){
$('#frm input[type="text"]').each(function(){
$(this).val("");
});
$('#frm input[type="radio":checked]').each(function(){
$(this).checked = false;
});
}
要么(纯js)
this.checked = false;
您不需要
each<\/code>功能
$("input:radio").attr("checked", false);
尝试
$(this).removeAttr('checked')
基于 Igor 代码对 Laurynas 插件的轻微修改。 这可以容纳与目标单选按钮相关的可能标签:
(function ($) {
$.fn.uncheckableRadio = function () {
return this.each(function () {
var radio = this;
$('label[for="' + radio.id + '"]').add(radio).mousedown(function () {
$(radio).data('wasChecked', radio.checked);
});
$('label[for="' + radio.id + '"]').add(radio).click(function () {
if ($(radio).data('wasChecked'))
radio.checked = false;
});
});
};
})(jQuery);
谢谢帕特里克,你让我开心! 这是你必须使用的mousedown。 但是,我改进了代码,以便您可以处理一组单选按钮。
//We need to bind click handler as well
//as FF sets button checked after mousedown, but before click
$('input:radio').bind('click mousedown', (function() {
//Capture radio button status within its handler scope,
//so we do not use any global vars and every radio button keeps its own status.
//This required to uncheck them later.
//We need to store status separately as browser updates checked status before click handler called,
//so radio button will always be checked.
var isChecked;
return function(event) {
//console.log(event.type + ": " + this.checked);
if(event.type == 'click') {
//console.log(isChecked);
if(isChecked) {
//Uncheck and update status
isChecked = this.checked = false;
} else {
//Update status
//Browser will check the button by itself
isChecked = true;
//Do something else if radio button selected
/*
if(this.value == 'somevalue') {
doSomething();
} else {
doSomethingElse();
}
*/
}
} else {
//Get the right status before browser sets it
//We need to use onmousedown event here, as it is the only cross-browser compatible event for radio buttons
isChecked = this.checked;
}
}})());
将 Igor 的代码重写为插件。
利用:
$('input[type=radio]').uncheckableRadio();
对于电台和电台组:
$(document).ready(function() {
$(document).find("input:checked[type='radio']").addClass('bounce');
$("input[type='radio']").click(function() {
$(this).prop('checked', false);
$(this).toggleClass('bounce');
if( $(this).hasClass('bounce') ) {
$(this).prop('checked', true);
$(document).find("input:not(:checked)[type='radio']").removeClass('bounce');
}
});
});
试试这个,这会成功:
$(document).ready(function() {
$("input[type='radio']").mousedown(function(e) {
if ($(this).attr("checked") == true) {
setTimeout("$('input[id=" + $(this).attr('id') + "]').removeAttr('checked');", 200);}
else {
return true
}
});
});
尝试
$(this).attr("checked" , false );
您还可以仅使用复选框来模拟单选按钮行为:
<input type="checkbox" class="fakeRadio" checked />
<input type="checkbox" class="fakeRadio" />
<input type="checkbox" class="fakeRadio" />
用这个
$("input[name='nameOfYourRadioButton']").attr("checked", false);
只需为 jQuery 输入以下代码:
jQuery("input:radio").removeAttr("checked");
您可以使用此 JQuery 取消选中单选按钮
$('input:radio[name="IntroducerType"]').removeAttr('checked');
$('input:radio[name="IntroducerType"]').prop('checked', false);
$('#frm input[type="radio":checked]').each(function(){
$(this).checked = false;
});
function setRadio(obj)
{
if($("input[name='r_"+obj.value+"']").val() == 0 ){
obj.checked = true
$("input[name='r_"+obj.value+"']").val(1);
}else{
obj.checked = false;
$("input[name='r_"+obj.value+"']").val(0);
}
}
<input type="radio" id="planoT" name="planoT[{ID_PLANO}]" value="{ID_PLANO}" onclick="setRadio(this)" > <input type="hidden" id="r_{ID_PLANO}" name="r_{ID_PLANO}" value="0" >
$('input[id^="rad"]').dblclick(function(){
var nombre = $(this).attr('id');
var checked = $(this).is(":checked") ;
if(checked){
$("input[id="+nombre+"]:radio").prop( "checked", false );
}
});
function clearForm(){
$('#frm input[type="text"]').each(function(){
$(this).val("");
});
$('#frm input[type="radio":checked]').each(function(){
$(this).attr('checked', false);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.