开发者

trigger('change') selects the 1st option, which I don't want

开发者 https://www.devze.com 2023-01-22 14:44 出处:网络
I have a select that has several options. No empty option. Before calling the trigger none of the options are selected, after it runs the first option is preselected, and I want that the select to st

I have a select that has several options. No empty option.

Before calling the trigger none of the options are selected, after it runs the first option is preselected, and I want that the select to stay as it was, no option selected

$("#selProduct").trigger('change');

How to fix this? I still want开发者_StackOverflow中文版 to trigger the change, to run the events, but I do not want the first option selected.


I think what you're seeing is the result of the DOM once it's touched and will depend on the browser (but selectedIndex should be 0 already).

That being, said, use .triggerHandler() to run just the handler, like this:

$("#selProduct").triggerHandler('change');

For your question though, you may want to store something on the <select> on page-load, that changes when it's "touched", for example:

$("#selProduct").change(function() { 
  $.data(this, 'changed', false);
  //do stuff
}).data('changed', false);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号