如何取消选中单选按钮?

本文翻译自: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("");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值