以编程方式更改值时触发 Dojo Select onChange 事件

发布于 2024-09-09 06:20:43 字数 698 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏蝶舞 2024-09-16 06:20:43

通常人们通过使用priorityChange标志来解决这个问题:

myWidget.set("value", 1234, false);

这将解决你的问题,除了一些微妙的问题,即值最初是123,你以编程方式将其设置为456,然后用户将其设置回123,在这种情况下不会有也可以是用户操作的 onChange() 事件。

因此,您还可以执行以下操作:

myWidget._lastValueReported=null;

Often people solve this by using the priorityChange flag:

myWidget.set("value", 1234, false);

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:

myWidget._lastValueReported=null;
赢得她心 2024-09-16 06:20:43

我认为在这种情况下,对您来说正确的解决方法是 http://bugs.dojotoolkit.org/ticket/ 10594,因为它直接处理 dijit.form.Select。当然,有几种方法可以解决这个问题。

  1. 升级道场:)。
  2. 继承 dijit.form.Select 并“修补”_updateSelection 函数。
  3. 扩展 dijit.form.Select 并直接在那里“修补”它。

我会放弃第一个。第二种和第三种方法类似,所以我将使用第三种方法发布一个简单的修复,

dijit.form.Select.extend({
   _updateSelection: function() {
        this.value = this._getValueFromOpts();
        var val = this.value;
        if(!dojo.isArray(val)){
            val = [val];
        }
        if(val && val[0]){
            dojo.forEach(this._getChildren(), function(child){
                var isSelected = dojo.some(val, function(v){
                    return child.option && (v === child.option.value);
                });
                dojo.toggleClass(child.domNode, this.baseClass + "SelectedOption", isSelected);
                dijit.setWaiState(child.domNode, "selected", isSelected);
            }, this);
        }
   }
});

请注意,我没有编写这个函数,我很高兴地从源代码中抄袭了它的最后一行, this._handleOnChange(this .value)已删除。

myWidget.attr('value', newValue, false) // should now work without firing onChange.

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.

  1. Upgrade dojo :).
  2. Inherit dijit.form.Select and "patch" the _updateSelection function.
  3. Extend dijit.form.Select and "patch" it directly there.

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,

dijit.form.Select.extend({
   _updateSelection: function() {
        this.value = this._getValueFromOpts();
        var val = this.value;
        if(!dojo.isArray(val)){
            val = [val];
        }
        if(val && val[0]){
            dojo.forEach(this._getChildren(), function(child){
                var isSelected = dojo.some(val, function(v){
                    return child.option && (v === child.option.value);
                });
                dojo.toggleClass(child.domNode, this.baseClass + "SelectedOption", isSelected);
                dijit.setWaiState(child.domNode, "selected", isSelected);
            }, this);
        }
   }
});

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.

myWidget.attr('value', newValue, false) // should now work without firing onChange.
宫墨修音 2024-09-16 06:20:43

一种更简单的方法是,我建议使用“_onChangeActive”标志,不建议使用该标志,因为它用于内部目的。但是,如果需要,我们可以使用它。 “_onChangeActive”是 dojo Select 类型小部件中存在的一个标志,默认设置为 true。
如果此标志设置为 true,则会照常触发 onChange 事件。但是,当它设置为 false 时,当用户或以编程方式更改值时,不会触发 onChange 事件。

例如:

var widget = registry.byId('widget_id');
widget.set('_onChangeActive', false); // setting the flag to false
...//make changes to the select programatically
widget.set('_onChangeActive',true); // setting back to true after programatic changes are done

无需为此继承现有的 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:

var widget = registry.byId('widget_id');
widget.set('_onChangeActive', false); // setting the flag to false
...//make changes to the select programatically
widget.set('_onChangeActive',true); // setting back to true after programatic changes are done

No need to inherit the existing dojo functionality for this or use separate external flags. The widget has one inbuilt for this - "_onChangeActive".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文