detect checkbox state change

前端 未结 2 1708
[愿得一人]
[愿得一人] 2021-01-04 02:30

I want to detect if user checks or unchecks a check box within an iframe and set a flag like so. But it does not seem to work as expected. Any pointers on the error on the c

相关标签:
2条回答
  • 2021-01-04 02:49

    Bind to the click event, not change. Checkboxes work on click.

    EDIT: It appears in modern browsers that changing the state of a checkbox, whether via mouse or keyboard, triggers at least the click and the change events.

    See a jsFiddle demo I built for proof. Using the keyboard also triggers some key-related events, in addition to click and change.

    0 讨论(0)
  • 2021-01-04 03:07

    Use the checked DOM property to check whether a checkbox is checked or not. The change event is always triggered on change, even if the checkbox gets unchecked.

    jQuery("#frameName").contents().find(":checkbox").bind('change', function(){        
            val = this.checked; //<---
            alert("changed");
    });
    
    0 讨论(0)
提交回复
热议问题