最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom events - How to stop onChange in JavaScript - Stack Overflow

programmeradmin0浏览0评论

In one of my selection boxes, I have an onChange="..." specified... because I want to change some other form value after any selection changes.

However, in the same page, some weird case I have to manually set the value. So I have to use some JavaScript to set the value of the selection combobox, but in this case, I don't want that onChange event to be fired.

How can I walk around it?

Forgot to mention that I am actually using dijit.formboBox. For normal HTML form comboBox, it won't cause any issue. Only I use the dijit comboBox, and I try to set the value to some other value, dojo will trigger the onChange.

In one of my selection boxes, I have an onChange="..." specified... because I want to change some other form value after any selection changes.

However, in the same page, some weird case I have to manually set the value. So I have to use some JavaScript to set the value of the selection combobox, but in this case, I don't want that onChange event to be fired.

How can I walk around it?

Forgot to mention that I am actually using dijit.form.comboBox. For normal HTML form comboBox, it won't cause any issue. Only I use the dijit comboBox, and I try to set the value to some other value, dojo will trigger the onChange.

Share Improve this question edited Feb 18, 2023 at 16:06 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 8, 2009 at 3:11 jojojojo 13.8k36 gold badges95 silver badges126 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 9

If you are using Dijit, then you can pass an additional false flag at the end of the set() method that will prevent the widget from firing the onChange event.

For example:

dijit.byId(myComboBox).set("value","Choose an option...",false);

Found this answer from Paul Christopher at http://dojo-toolkit.33424.n3.nabble.com/onchange-event-firing-when-setting-value-of-a-Select-programmatically-td3985692.html. It worked perfectly!

myDigit._lastValueReported = myValue;
myDigit.set('value', myValue);

You don't need to do anything. Setting the value with Javascript will not fire your onchange event handler.

In general, setting the value with JavaScript won't fire onchange. If you're dealing with a strange browser that does fire it, you could remove the onChange (element.onchange = null), change the value, then add it back (element.onchange = functionname) afterwards.

FYI, this answer is not fully correct. It is true that simply setting the value does not trigger the onChange event, BUT as soon as the control loses focus, the change will be detected and onChange will be fired.

So delaying onChange is not really the same as preventing onChange - which is what I need to do!

I could temporarily remove the event, blur and refocus the field, and then restore the event, but this is an ugly hack. It is complicated by dynamicaly added events like jQuery. so really what I'd like is to set the 'focus value' to the 'new value', but haven't been able to find this. I could try setting the defaultValue, but this would prevent a correct form.reset().

发布评论

评论列表(0)

  1. 暂无评论