发布于 2024-12-11 18:55:55 字数 402 浏览 0 评论 0原文

通常,当用户选择 的值时,该事件不会被触发。

我知道我可以这样做: $('select').val('something').trigger('change'); 但这不是我想要解决的问题...

有没有办法让更改事件正常工作,而无需手动触发它?

我整理了一个快速的 JsFiddle 来更好地解释问题,请查看:

http://jsfiddle.net/W723K/ 1/

干杯

Normally, when a user selects an item in a <select>, the 'change' event gets fired.
However, when you change the value of the same <select> with $('select').val('something'), the event doesn't get fired.

I know I could do:
$('select').val('something').trigger('change');
but that's not the problem I'm trying to solve...

Is there a way to get the change event working, without manually triggering it?

I put together a quick JsFiddle to better explain the problem, check it out:

http://jsfiddle.net/W723K/1/

Cheers

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

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

发布评论

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

评论(1

微暖i 2024-12-18 18:55:55

这是不可能的,除非您手动触发更改功能。如果您不喜欢多次键入该代码,请扩展 jQuery 对象。 小提琴:http://jsfiddle.net/W723K/2/

(function($){
    $.fn.changeVal = function(value){
        return this.each(function(){
            $(this).val(value).trigger('change');
        });
    }
})(jQuery);

//Usage:
$('select').changeVal('something');

It's not possible, unless you manually trigger the change function. If you don't like typing that code several times, extend the jQuery object. Fiddle: http://jsfiddle.net/W723K/2/

(function($){
    $.fn.changeVal = function(value){
        return this.each(function(){
            $(this).val(value).trigger('change');
        });
    }
})(jQuery);

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