触发器('change') 选择第一个选项,这是我不想要的

发布于 2024-09-29 03:50:00 字数 231 浏览 0 评论 0原文

我有一个有多个选项的选择。没有空选项。

调用触发器之前,没有选择任何选项,之后它运行时,第一个选项被预先选择,我希望选择保持原样,没有选择任何选项

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

如何解决这个问题? 我仍然想触发更改以运行事件,但我不想选择第一个选项。

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 to trigger the change, to run the events, but I do not want the first option selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只是我以为 2024-10-06 03:50:01

我认为您看到的是 DOM 被触摸后的结果,并且取决于浏览器(但是 selectedIndex 应该 已经是 0 )。

也就是说,使用 .triggerHandler() 来运行 只是处理程序,如下所示:

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

对于您的问题,您可能希望在页面加载时在

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

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