Get value of checkbox in dojo, with on() method

 ̄綄美尐妖づ 提交于 2019-12-12 02:29:16

问题


Using dojo 1.9, I'm trying to access the value of a checkbox not like in the example in the docs:

require(["dijit/form/CheckBox", "dojo/domReady!"], function(CheckBox){ 
    var checkBox = new CheckBox({ 
        name: "checkBox", 
        value: "agreed", 
        checked: false, 
        onChange: function(b){ alert('onChange called with parameter = ' + b + ', and     widget value = ' + this.get('value') ); } 
    }, "checkBox"); 
}); 

but by moving onChange event to:

query('#checkBox').on('change', function(){ 
    var value = query(this).attr('value'); 
}); 

or anything similar. I just want to access it from on() method. But I get the same value every time - checkbox is checked. Any ideas?


回答1:


And again I made the same mistake - it should be

registry.byId('checkBox').on('change', function(){ 
    var value = this.value; 
});



回答2:


dojo/query returns plain dom Nodes of the widgets, and therefore, we cannot attach the "on" event handler, directly to the query result. As you have said, we can use registry.byId(...)

In addition, let me give a suggestion, where the query would return more number of dom nodes.

array.forEach(dojo.query('.classname'),function(node){
  dijit.getEnclosingWidget(node).on('change',function(){
    var value = this.value;
    console.info(value);
  });
});

dijit.getEnclosingWidget(domNode) - will give the immediate parent widget node of the mentioned 'domNode'



来源:https://stackoverflow.com/questions/18442594/get-value-of-checkbox-in-dojo-with-on-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!