开发者

Is it possible to deselect all values in a RadioButton Group?

开发者 https://www.devze.com 2023-02-15 11:59 出处:网络
I have a form in an ASP.NET MVC site which the user can edit and return to at a later time. If they accidentally select a value in a radio button group, is there any way to make it unselected?

I have a form in an ASP.NET MVC site which the user can edit and return to at a later time.

If they accidentally select a value in a radio button group, is there any way to make it unselected?

By default, once it's 开发者_C百科clicked, it can't be unclicked!


You can do it with plain javascript. Example:

function clearRadios(groupName) {
  var r = document.getElementsByName(groupName);
  for (var i = 0; i < r.length; i++) {
    r[i].checked = false;
  }
}
<label><input type="radio" name="myGroup">A</label>
<label><input type="radio" name="myGroup">B</label>
<label><input type="radio" name="myGroup">C</label>
<button type="button" onclick="clearRadios('myGroup')">Clear</button>


You have two options, create another radio button labelled "None" or something appropriate to your form that's part of the same 'group' then they can select that instead.

But in reality, if you need them to be able to deselect then you need a checkbox and not a radio button.

0

精彩评论

暂无评论...
验证码 换一张
取 消