Only one selected checkbox

后端 未结 8 1789
梦毁少年i
梦毁少年i 2021-01-14 05:17

I have 15 check boxes in a form. This checkboxes are independent to eachother. I want a javascript function that makes, when user is selecting 2 checkboxes, the first checke

8条回答
  •  不思量自难忘°
    2021-01-14 05:28

    I created this CodePen off of John's answer, but I added the ability to uncheck all checkboxes.

    HTML:



    JavaScript:

    function selectOnlyThis(id) {
    for (var i = 1;i <= 4; i++){
        if ("Check" + i === id && document.getElementById("Check" + i).checked === true){
                document.getElementById("Check" + i).checked = true;
                } else {
                  document.getElementById("Check" + i).checked = false;
                }
        }  
    }
    

    https://codepen.io/thomasweld/pen/Pmaega?editors=1010

提交回复
热议问题