http://jsfiddle.net/0n04s1Ls/1/

HTML:

<input type="radio" name='abc1' class="rad" /> <input type="radio" name='abc2' class="rad" /> <input type="radio" name='abc3' class="rad" /> 

Js:

 $('.rad').mousedown(function () { $(this).data('checked', $(this).prop('checked')); }).click(function () { var checked = $(this).data('checked'); $('.rad').prop('checked', false); $(this).prop('checked', !checked); }); 

Task: do not give to choose more than one input. Give the ability to deselect. It seems that this shit. But I don’t know how to significantly improve it.

My options are:
1) Still, to call them one name - no need for everyone to put false;
2) use the checkbox - the need to check the status of the input will disappear, but everyone will have to put false;

Is there a human solution?

  • 2
    @knes, and is it not the way out to give one name to all three fields? - Deonis
  • jsfiddle.net/0n04s1Ls/2 - not. Only one line is missing. And I would like to at least get rid of one event. This can be done using the checkbox. - knes
  • @knes, here I was [about this] [1] said [1]: jsfiddle.net/Deonis/0n04s1Ls/3 - Deonis
  • In the end, if you really need different names, then [it is possible and so] [1]: var rad = $ ('. Rad'); rad.on ('change', function () {rad.not (this) .prop ('checked', false);}); [1]: jsfiddle.net/Deonis/0n04s1Ls/5 - Deonis
  • In chrome and ff you can not remove the selection - knes

1 answer 1

@knes , this is an extraordinary task, so I would not say that you have some kind of barn code. It can be written differently , but the meaning does not change. Well, and still fields give one name:

 var stat = false; $('.rad').on({ mousedown: function(){ stat = $(this).prop('checked'); }, click: function(e){ if(stat) $(this).prop('checked', false); } });