本文翻译自:How to uncheck a radio button?
I have group of radio buttons that I want to uncheck after an AJAX form is submitted using jQuery. 在使用jQuery提交AJAX表单后,我想要取消选中一组单选按钮。 I have the following function: 我有以下功能:
function clearForm(){
$('#frm input[type="text"]').each(function(){
$(this).val("");
});
$('#frm input[type="radio":checked]').each(function(){
$(this).checked = false;
});
}
With the help of this function, I can clear the values at the text boxes, but I can't clear the values of the radio buttons. 在此功能的帮助下,我可以清除文本框中的值,但我无法清除单选按钮的值。
By the way, I also tried $(this).val("");
顺便说一句,我也试过$(this).val("");
but that didn't work. 但那没用。
#1楼
参考:https://stackoom.com/question/8srq/如何取消选中单选按钮
#2楼
For radio and radio group: 对于无线电和无线电组:
$(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');
}
});
});
#3楼
$('#frm input[type="radio":checked]').each(function(){
$(this).checked = false;
});
This is almost good but you missed the [0] 这几乎是好的,但你错过了[0]
Correct ->> $(this)[0].checked = false;
正确 - >> $(this)[0].checked = false;
#4楼
尝试
$(this).attr("checked" , false );
#5楼
either (plain js) 要么(普通的js)
this.checked = false;
or (jQuery) 或者(jQuery)
$(this).prop('checked', false);
// Note that the pre-jQuery 1.6 idiom was
// $(this).attr('checked', false);
See jQuery prop() help page for an explanation on the difference between attr() and prop() and why prop() is now preferable. 有关attr()和prop()之间差异的解释以及为什么prop()现在更可取,请参阅jQuery prop()帮助页面 。
prop() was introduced with jQuery 1.6 in May 2011. prop()于2011年5月与jQuery 1.6一起推出。
#6楼
You wouldn't need the each
function 你不需要each
功能
$("input:radio").attr("checked", false);
Or 要么
$("input:radio").removeAttr("checked");
The same should also apply to your textbox: 同样也适用于您的文本框:
$('#frm input[type="text"]').val("");
But you could improve this 但你可以改善这一点
$('#frm input:text').val("");