简体   繁体   English

无法取消HTML复选框的onchange事件

[英]Can't cancel the onchange event for my HTML checkbox

I have a working example of this but I'm not sure why my implementation isn't working. 我有一个这样的工作示例,但我不确定为什么我的实现不起作用。 Can somebody please help? 有人可以帮忙吗? I'm missing something and I need another set of eyes to find it for me. 我错过了一些东西,我需要另一组眼睛才能找到它。 :) :)

JSFiddle demo <-- see code here. JSFiddle演示 < - 请参阅此处的代码。

I'm trying to reproduce the functionality used here . 我正在尝试重现此处使用的功能。 I seem to be a little confused about passing/accessing the event variable around. 我似乎对传递/访问event变量有点困惑。

Thanks!! 谢谢!! :-) :-)

Bind to the Click event: example 绑定到Click事件: 示例

$(document).ready(function() {
    $('#checkbox1').click(function(event) {
        if (flag) {
            CancelEvent(event);
            return event.returnValue;
        }
        SomeOtherStuff('this method has real functionality too');
    });
});​

Change is a little to late in the event structure to cancel the action that already happened. 事件结构中的更改有点迟到,以取消已经发生的操作。 In addition, the return value was being omitted. 另外,返回值被省略。 So I added it to the event handler. 所以我将它添加到事件处理程序中。

You forgot to add return event.returnValue; 你忘了添加return event.returnValue; and onClick will work instead of onChange? 和onClick将工作而不是onChange?

http://jsfiddle.net/wanUF/11/ http://jsfiddle.net/wanUF/11/

Working jsfiddle . 工作jsfiddle

return false on checkbox click. 单击复选框时返回false。

$('#checkbox1').click(function(event) {

    return false;
});

you can either use disabled attribute of input 你可以使用输入的禁用属性

<input type="checkbox" id="checkbox1" disabled=disabled  />

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

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