Change event doesn't get called when the value of <select> is set programmatically

后端 未结 1 1413
傲寒
傲寒 2020-12-04 03:01

Normally, when a user selects an item in a <

相关标签:
1条回答
  • 2020-12-04 03:21

    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');
    
    0 讨论(0)
提交回复
热议问题