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

前端 未结 7 2291
遥遥无期
遥遥无期 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 09:52

    We can do this using JavaScript, no need of jQuery. Just pass the changed element and let JavaScript handle it.

    HTML

    syn

    JS

    function doalert(checkboxElem) {
      if (checkboxElem.checked) {
        alert ("hi");
      } else {
        alert ("bye");
      }
    }
    

    Demo Here

提交回复
热议问题