perform an action on checkbox checked or unchecked event on html form

前端 未结 7 2333
遥遥无期
遥遥无期 2020-12-06 09:40

I have a checkbox on a form which is unchecked by default as usual. now I want to perform two separated actions on checked and unchecked state of this checkbox.

this

7条回答
  •  无人及你
    2020-12-06 10:03

    If you debug your code using developer tools, you will notice that this refers to the window object and not the input control. Consider using the passed in id to retrieve the input and check for checked value.

    function doalert(id){
      if(document.getElementById(id).checked) {
        alert('checked');
      }else{
        alert('unchecked');
      }
    }
    

提交回复
热议问题