简体   繁体   English

复选框状态更改检测

[英]Checkbox state change detection

I have a scenario in which i am showing/Hiding a portion of the screen depending on whether the check box is checked or now. 我有一种情况,根据该复选框是选中还是现在,我正在显示/隐藏屏幕的一部分。

So the catch here is that, Not only the user will be changing the state of the check box, but also programmatically. 因此,这里的问题在于,不仅用户会更改复选框的状态,而且还会以编程方式更改。

So how do i detect this change and when ever the state of check box is changed , i want to section to be hidden or shown. 因此,我如何检测到此更改,并且无论何时复选框的状态发生更改,我都想隐藏或显示该部分。

Presently i am doing like this... 目前我正在这样做...

    externalCheckBoxClicked : function (e){
        var $target = $(e.target),
          checked = $target.prop('checked');

        if(checked){
            $('#confirm-button').show();
        }else{
            $('#confirm-button').hide();
        }
    }

    setToPreviousSetValues : function(){
        if(this.requestData.isOverrideExternalParams === "1"){
            $('#ExternalParametersChkBox').prop('checked',true);
        }else {
            $('#ExternalParametersChkBox').prop('checked',false);
        }
    },

   events: {
       "change #ExternalParametersChkBox":"externalCheckBoxClicked",
    },

Any Clue why this is not working. 任何线索为什么这不起作用。

$('#yourID').change(function() {
  // your code
});

you can try this one: 您可以尝试以下一种方法:

$(".checkbox").change(function() {
    if(this.checked) {
        //Do stuff
    }
});

You can add .checkbox this place Something Class File Or Id file 您可以在此位置添加.checkbox东西Class文件或Id文件

<input type="checkbox" id="something" />

$("#something").click( function(){
   if( $(this).is(':checked') ) alert("checked");
});

Doing this will not catch when the checkbox changes for other reasons than a click, like using the keyboard. 如果复选框由于单击以外的其他原因而发生更改(例如使用键盘),则不会执行此操作。 To avoid this problem, listen to changeinstead of click. 为避免此问题,请听更改而不是单击。

Source: Catch checked change event of a checkbox 源: 捕获复选框的选中的更改事件

For programmatically changing: Why isn't my checkbox change event triggered? 对于以编程方式进行的更改: 为什么未触发我的复选框更改事件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM