unable to uncheck the radiobutton

蹲街弑〆低调 提交于 2019-12-14 03:34:00

问题


Hi when i click the radion button ,It always checked.I cannot uncheck the radion button am serching and try How to check/uncheck radio button on click? this link.

problems

I can check only once,once i click uncheck it always uncheck cannot work properly code

  <input value='' type='radio' id='one' onClick="getCurrentLocation()"/>

   $('input[type=radio]').click(function(){

    if (this.previous) {
        this.checked = false;
    }
    this.previous = this.checked;
});

回答1:


Is it your the only radio button?

Radio buttons vere developed for keeping one of possible states from the set.

So if there is only one radio button - change it with checkbox.

If you have several of them - choose some default one for unchecking currently selected. But if you click on another radio from the set, it will became checked without need of special code.

(As far as I remember, there was no way to uncheck radio-button in browser with mouse click on it, so I don't expect such behavour from scripts as well)

For checkbox you don't need special code to uncheck it on click - it's default behavour for them in all browsers!

If it's only dessign question, then you can use IMG of empty and checked radio buttons, and onClick change the image. In this case you can keep your state in hidden input field.

But it will be confusing for your users...




回答2:


The easiest way is to use same name property input.

example:

<input name="myname" value='' type='radio' id='one' onClick="getCurrentLocation()"/>
<input name="myname" value='' type='radio' id='two' onClick="getCurrentLocation()"/>

DEMO

As you need single check at a time, so it would be better to use checkbox.

<input name="myname" value='' type='checkbox' id='one' onClick="getCurrentLocation()"/>
<input name="myname" value='' type='checkbox' id='two' onClick="getCurrentLocation()"/>



回答3:


That's how they work. Radio buttons should be user in groups, where you choose between them. If you want a single button that you can toggle, use a checkbox.

If you are trying to mimic a checkbox using a radio button, you will just confuse your users.




回答4:


try this,

markup:

<input value='' type='radio' id='one' onClick="getCurrentLocation()"/>

jQuery,

var flag = 0;
$('input[type=radio]').click(function(){
    if(flag === 0)
    {
      $(this).prop('checked', true);              
      flag = 1;
    }
    else if(flag == 1)
    {
      $(this).prop('checked', false);
      flag = 0;        
    }

});

Since you're using an id and have only one radio button call it directly, you don't need to use this

Although, I'd recommend you use checkbox like the others have.

Demo



来源:https://stackoverflow.com/questions/10764352/unable-to-uncheck-the-radiobutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!