以编程方式更改值时触发 Dojo Select onChange 事件
我有一个 dojo (dijit) 选择下拉列表,它调用 js 函数 onChange。我原以为只有当用户更改下拉列表中的值时才会调用 onChange 函数,但是,当我以编程方式从 js 代码更改下拉列表的值时,它甚至会调用 onChange 函数。如何让它仅在用户更改下拉值时调用该函数?当我以编程方式更改值时,它不应该调用该函数。
<select jsId="ddlBoundaryType" id="ddlBoundaryType" name="ddlBoundaryType"
dojoType="dijit.form.Select">
<option value="0">Circle</option>
<option value="1">Polygon</option>
</select>
dojo.addOnLoad(InitBoundaries);
function InitBoundaries() {
dojo.connect(dijit.byId("ddlBoundaryType"), 'onChange', Boundaries_ChangeBoundaryType);
}
I have a dojo (dijit) select dropdown that calls a js function onChange. I was expecting this to only call the onChange function when the user changes the value in the dropdown, however, it even calls the onChange function when I programmatically change the value of the dropdown from js code. How do I get it to only call the function when the user changes the dropdown value? It shouldn't call the function when I programmatically change the value.
<select jsId="ddlBoundaryType" id="ddlBoundaryType" name="ddlBoundaryType"
dojoType="dijit.form.Select">
<option value="0">Circle</option>
<option value="1">Polygon</option>
</select>
dojo.addOnLoad(InitBoundaries);
function InitBoundaries() {
dojo.connect(dijit.byId("ddlBoundaryType"), 'onChange', Boundaries_ChangeBoundaryType);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常人们通过使用priorityChange标志来解决这个问题:
这将解决你的问题,除了一些微妙的问题,即值最初是123,你以编程方式将其设置为456,然后用户将其设置回123,在这种情况下不会有也可以是用户操作的 onChange() 事件。
因此,您还可以执行以下操作:
Often people solve this by using the priorityChange flag:
That will solve your problem except for subtle issues where the value is originally 123, you set it programatically to 456, and then the user sets it back to 123, in which case there won't be an onChange() event for the user action either.
For that reason you can additionally do:
我认为在这种情况下,对您来说正确的解决方法是 http://bugs.dojotoolkit.org/ticket/ 10594,因为它直接处理 dijit.form.Select。当然,有几种方法可以解决这个问题。
我会放弃第一个。第二种和第三种方法类似,所以我将使用第三种方法发布一个简单的修复,
请注意,我没有编写这个函数,我很高兴地从源代码中抄袭了它的最后一行, this._handleOnChange(this .value)已删除。
I think the proper fix in this case for you would be http://bugs.dojotoolkit.org/ticket/10594, since it deals directly with dijit.form.Select. Of course, there are a few ways to fix this.
I will forgo the first. The the second and the third method are similar, so I will just post a simple fix using the third way,
Note that I did not write this function, I happily plagiarized it from the source code with the last line, this._handleOnChange(this.value) removed.
一种更简单的方法是,我建议使用“_onChangeActive”标志,不建议使用该标志,因为它用于内部目的。但是,如果需要,我们可以使用它。 “_onChangeActive”是 dojo Select 类型小部件中存在的一个标志,默认设置为 true。
如果此标志设置为 true,则会照常触发 onChange 事件。但是,当它设置为 false 时,当用户或以编程方式更改值时,不会触发 onChange 事件。
例如:
无需为此继承现有的 dojo 功能或使用单独的外部标志。该小部件为此内置了一个 - “_onChangeActive”。
A much simpler way is that, I would like to suggest the "_onChangeActive" flag, which is not recommended to be used, as it is serves internal purpose. But, if required, we could use it. "_onChangeActive" is a flag present in dojo Select type widgets, which is by default set to true.
If this flag is set to true, the onChange event is triggered as usual. But, when it is set to false, the onChange event is not triggered, when the value is changed either by user or programatically.
e.g:
No need to inherit the existing dojo functionality for this or use separate external flags. The widget has one inbuilt for this - "_onChangeActive".