使用Google Closure从复选框激活组合框 [英] Activate a combobox from a checkbox using google closure
问题描述
在对话框中,我有一组带有ID的user1,user2等的复选框,还有一组带有ID的userel1,usersel2等的组合框.选中复选框时(例如,假设使用id user1),则必须激活相应的组合框(即,具有id usersel1的组合框).我有以下代码,但无法正常工作.我该如何实现这种行为?
I have a set of check boxes with Id's user1,user2 and so on and also set of combo boxes with Id's usersel1,usersel2 and so on in a dialog. When a check box is checked (say suppose with Id user1) then corresponding combo box must be activated(i.e combo box with Id usersel1). I have the following code and isn't working. How do i achieve this behavior?
for(var g=0;g<userlist.length;g++) //userlist.length give no of users
b2 = (goog.dom.getElement('usersel'+(g+1))); //gets combo box
//listening if check box is clicked
goog.events.listen(goog.dom.getElement('user'+(g+1)),
goog.events.EventType.CLICK,
function(e) {
b2.disabled = (false); // trying to enable corresponding combo box
});
上面这段代码的问题在于,只有最后一个组合框被激活,才能单击任何复选框.
The problem with above piece of code is that any check box is clicked only the last combo box gets activated.
推荐答案
有几个问题.
首先,for
循环仅应用于第二行,而不应用于整个块.
First, the for
cycle is applied only to the second line, not to the whole block.
Second, the way of creating the closure is wrong (see here). The value b2 is not propagated into the event handlers as you would think.
如果这样重写,它将起作用:
It would work if rewritten like this:
for (var g=0;g<userlist.length;g++) { //userlist.length give no of users
b2 = goog.dom.getElement('usersel'+(g+1)); //gets combo box
//listening if check box is clicked
goog.events.listen(goog.dom.getElement('user'+(g+1)),
goog.events.EventType.CLICK,
makeEventHandler(b2)
);
}
function makeEventHandler(element) {
return function(e) {
element.disabled = false;
}
}
这篇关于使用Google Closure从复选框激活组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!