How to check/uncheck radio button on click?

后端 未结 18 1392
情深已故
情深已故 2020-11-29 02:16

I want to be able to uncheck a radio button by clicking on it.

So, if a radio button is unchecked, I want to check it, if it is checked, I want to uncheck it.

<
18条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 02:39

    Found this somewhere while looking for a solution, do like the simplicity of it...

    var checkedradio;
    function docheck(thisradio) {
        if (checkedradio == thisradio) {
            thisradio.checked = false;
            checkedradio = null;
        }
        else {checkedradio = thisradio;}
    }
    

    Use with:

    
    

    It does seem to require a double click to deselect when you have multiple radio groups in a form though, but this could by solved by having a function for each group I suppose...

提交回复
热议问题