最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncheck Radio Button in reactjs - Stack Overflow

programmeradmin0浏览0评论

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=".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>

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./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>

Share Improve this question edited Jul 25, 2018 at 8:31 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jul 25, 2018 at 8:26 Ambika TewariAmbika Tewari 2513 silver badges9 bronze badges 5
  • $('input[type="radio"]').prop('checked', false); should work. – Ankit Agarwal Commented Jul 25, 2018 at 8:27
  • Where are you calling this code? If it's just in the outer javascript, it will be executed immediately onload. – Liftoff Commented Jul 25, 2018 at 8:29
  • You should use checkbox instead – Zakaria Acharki Commented Jul 25, 2018 at 8:32
  • Your code is definitely right. But I wondered whether you call it at the right place or not. – Terry Wei Commented Jul 25, 2018 at 8:33
  • Hi everyone, this code is written on top of jaavscript. – Ambika Tewari Commented Jul 25, 2018 at 9:19
Add a ment  | 

2 Answers 2

Reset to default 4

You don't need jQuery for this. 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.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. (Link)

Here's an example to show how they works.(Only first time set attribute works)

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./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>

发布评论

评论列表(0)

  1. 暂无评论