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

前端 未结 7 2284
遥遥无期
遥遥无期 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:43

    The currently accepted answer doesn't always work.

    (To read about the problem and circumstances, read this: Defined function is "Not defined".)

    So, you have 3 options:

    1 (it has above-mentioned drawback)

    <input type="checkbox" onchange="doAlert(this)">
    
    <script>
    function doAlert(checkboxElem) {
        if (checkboxElem.checked) {
            alert ('hi');
        } else {
            alert ('bye');
        }
    }
    </script>
    

    2 and 3

    <input type="checkbox" id="foo">
    
    <script>
    function doAlert() {
        var input = document.querySelector('#foo');
    
        // input.addEventListener('change', function() { ... });
        // or
        // input.onchange = function() { ... };
        input.addEventListener('change', function() {
            if (input.checked) {
                alert ('hi');
            } else {
                alert ('bye');
            }
        });
    }
    
    doAlert();
    </script>
    
    0 讨论(0)
  • 2020-12-06 09:45
    <form>
        syn<input type="checkbox" name="checkfield" id="g01-01" />
    </form>
    

    js:

    $('#g01-01').on('change',function(){
        var _val = $(this).is(':checked') ? 'checked' : 'unchecked';
        alert(_val);
    });
    
    0 讨论(0)
  • 2020-12-06 09:48

    Given you use JQuery, you can do something like below :

    HTML

    <form id="myform">
        syn<input type="checkbox" name="checkfield" id="g01-01"  onclick="doalert()"/>
    </form>
    

    JS

    function doalert() {
      if ($("#g01-01").is(":checked")) {
        alert ("hi");
      } else {
        alert ("bye");
      }
    }
    
    0 讨论(0)
  • 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

    <form id="myform">
        syn<input type="checkbox" name="checkfield" id="g01-01"  onchange="doalert(this)"/>
    </form>
    

    JS

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

    Demo Here

    0 讨论(0)
  • 2020-12-06 09:53

    Have you tried using the JQuery change event?

    $("#g01-01").change(function() {
        if(this.checked) {
            //Do stuff
        }
    });
    

    Then you can also remove onchange="doalert(this.id)" from your checkbox :)

    Edit:

    I don't know if you are using JQuery, but if you're not yet using it, you will need to put the following script in your page so you can use it:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    
    0 讨论(0)
  • 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');
      }
    }
    
    0 讨论(0)
提交回复
热议问题