在通过编程方式更改html选择下拉菜单后,onchange不会启动 [英] onchange wont fire after programmatically changing html select dropdown
问题描述
我在HTML内部选择
< select id =leaguename =league>
我正在收听我的javascript内的更改。
var league = dojo.byId(league);
dojo.connect(league,onchange,function(evt){// do stuff}
但是我有一个链接可以点击选择的更新:
< ; a href = javascript:void(0)onclick =updateSelection(league);>联盟< / a>
链接用于使用以下功能更新所选择的值。
function updateSelection( NewLeague){
/ pre>
dojo.byId('league')。value = NewLeague; // works
dojo.byId('league')。onChange; //这是不工作
// dojo .byId('league')。onChange(); // this throws:TypeError:dojo.byId(league)。onChange不是一个函数
}
我的问题,正如我通过其他堆栈帖子阅读的那样,以编程方式更新值不会触发onChange,因此我需要在代码中调用onchange根据线内注释,onChange不是bei ng触发或引发错误。我的第一个想法是它与dojo.Connect有关,它监听onChange,但是我没有发现任何说明不能的信息,也没有任何解释如何解决。 p>
任何想法?
解决方案选择onchange不会触发程式修改你需要用
league.onchange();
应该是小写的。
另外,我不知道dojo是否有一个触发方法,但是在jQuery中,这将作为
jQuery(' #league')。trigger('change')
。
根据您的dojo版本,您可能还需要检查: http://dojotoolkit.org/reference-guide/1.8/dojo/connect.html
I have a select inside HTML
<select id="league" name="league">
which I'm listening for changes inside my javascript.
var league = dojo.byId("league"); dojo.connect(league, "onchange", function (evt) { //do stuff }
Which works fine. However I have a link that I can click which updates the select:
<a href=javascript:void(0) onclick="updateSelection(league);"> League </a>
The link works as it updates the selected value of the select with the following function.
function updateSelection(NewLeague){ dojo.byId('league').value = NewLeague; // works dojo.byId('league').onChange; //this isnt working //dojo.byId('league').onChange(); //this throws: TypeError: dojo.byId("league").onChange is not a function }
My problem, as I've read through other stack posts is that programmatically updating the value wont trigger onChange, thus I need to call onchange in the code (shown above). As per the comments inline, the onChange isn't being triggered or throws an error. My first thought that it has something to do with the dojo.Connect which listens for onChange, but I havent found any information that says I cant do this, nor any explanation how to get around it.
Any ideas?
解决方案Select onchange doesn't fire for programattic changes, you need to fire it yourself with
league.onchange();
As noted by @Greg, the call should be lowercase.
Additionally, I don't know if dojo has a trigger method, but in jQuery this would be done as
jQuery('#league').trigger('change')
.Depending on your version of dojo you may also want to check: http://dojotoolkit.org/reference-guide/1.8/dojo/connect.html
这篇关于在通过编程方式更改html选择下拉菜单后,onchange不会启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!