简体   繁体   English

取消选中reactjs中的单选按钮

[英]Uncheck Radio Button in reactjs

I want to uncheck a radio button and I have tried almost all below methods but those are not working. 我想取消选中一个单选按钮,我已经尝试了几乎所有下面的方法,但那些都没有用。

Can someone advise me how to achieve this ? 有人可以告诉我如何实现这一目标吗?

 $('input[type="radio"]').prop('checked', false); $('input[type="radio"]').removeAttr("checked"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="savercol text-center" id="regfareId00"> <span class="star icon-round_trip"></span> <input type="radio" class="" name="foo0" value="0~P~~P0IPRT~3207~~29~X|6E~2135~ ~~BLR~07/28/2018 01:30~DEL~07/28/2018 04:15~"> <label class=""><span>₹&nbsp;3,344</span></label> </div> 

You don't need jQuery for this. 你不需要jQuery。 If you only have one option you should be using a checkbox, if radio buttons then you can use something like below: 如果你只有一个选项,你应该使用一个复选框,如果单选按钮,那么你可以使用如下所示:

Set a state 设置一个州

this.state = {
   value: ''
};

Change handler 变更处理程序

handleChange(e) {
   value: e.target.value
}

In render, where options is an array of your values. 在渲染中,options是值的数组。

{options.map((option, i) => (
   <div>
      <input
         type="radio"
         name={name}
         value={option.value}
         checked={this.state.value === option.value}
         onChange={this.handleChange}}
      />
   </div>
))}

In jQuery you should know the difference between attribute and property . jQuery您应该知道attributeproperty之间的区别。 ( Link ) 链接

Here's an example to show how they works.(Only first time set attribute works) 这是一个展示它们如何工作的例子。(只有第一次设置attribute有效)

Hope this helps :) 希望这可以帮助 :)

 $('#a').click(function(){ $('input[type="radio"]').attr("checked", true); }) $('#b').click(function(){ $('input[type="radio"]').removeAttr("checked"); }) $('#c').click(function(){ $('input[type="radio"]').prop('checked', true); }) $('#d').click(function(){ $('input[type="radio"]').prop('checked', false); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="savercol text-center" id="regfareId00"> <span class="star icon-round_trip"></span> <input type="radio" class="" name="foo0" value="0~P~~P0IPRT~3207~~29~X|6E~2135~ ~~BLR~07/28/2018 01:30~DEL~07/28/2018 04:15~"> <label class=""><span>₹&nbsp;3,344</span></label> </div> <button id="a">Set by .attr</button> <button id="b">Remove by .removeAttr</button> <button id="c">Set by .prop true</button> <button id="d">Remove by .prop false</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM