trigger('change') selects the 1st option, which I don't want
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);
精彩评论