1

I have an html list of checkboxes but I don't know how to get the 'document.attachEvent' to find those checkboxes on an onclick?

I wasn't sure if I could set the event model specific to the IE then in a document.attachEvent create a for loop that goes through every checkbox and handles each one? Also, my checkboxes are of all different names so I can't checkboxname.attachEvent unless I did that to each one.

My elements are dynamics enough that I tried adding en event to the broadest ancestor, which was the document to that I could use the event to get the target and type with no avail.

Much Thanks.

4

1 回答 1

1

对您的代码的一些修复:

document.attachEvent('onclick', function (e) {
    var target = e.srcElement;
    if (target.type === 'checkbox') {
        if(target.checked){
            button.disabled = false;
        } else {
            button.disabled = true;
        }       
    } 
});

IE 的事件处理模型中没有使用第三个参数。e.srcElement指点击的元素。

我建议您将复选框包装在一个div或其他一些元素中,然后将事件侦听器附加到包装器。当您的页面变大时,检查文档上的所有点击将是一项耗时的操作。如果您只有一个复选框,当然最好将处理程序附加到自身。

于 2013-07-31T15:40:21.053 回答