简体   繁体   English

jQuery onchange不触发

[英]Jquery onchange does not trigger

$('#selectclassid').trigger("change");
    $('#selectclassid').on('change', function() {
});

I have this format for my onchange manual trigger of on change but the problem is it is not firing. 我的onchange手动触发器具有on更改的这种格式,但问题是它没有触发。 I am wondering because this is the format I see in tutorials. 我想知道,因为这是我在教程中看到的格式。 Could there be anything wrong in the format of on change. 变更的格式可能有任何错误。 I am hoping that it will fire on change when the page is loaded. 我希望加载页面时,它将在更改时触发。 By the way this select option is in a div that is hidden on load.But will show on button click if this event is related to the problem. 顺便说一下,此选择选项位于加载时隐藏的div中,但如果此事件与问题有关,将在按钮单击时显示。 Any idea is appreciated 任何想法表示赞赏

Hm, you seem to be triggering the event before you attach it - try it the other way around 嗯,您似乎附加事件之前就已经触发了该事件-请尝试另一种方法

 $('#selectclassid').on('change', function() {
     console.log("fired")
 });

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

https://jsfiddle.net/ce5rg9mt/ https://jsfiddle.net/ce5rg9mt/

$( document ).ready(function() {




$("#selectclassid").on('change', function() {
    alert('The option with value ' + $(this).val());
});


     $( "#selectclassid" ).trigger( "change" );

});

Wrap your code inside of the callback function like so: 将代码包装在回调函数中,如下所示:

$('#selectclassid').on('change', function(){
    //code here
});

And then call the trigger afterwards: 然后再调用触发器:

$('#selectclassid').trigger('change');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM