开发者

trigger the change event in jquery, weird behaviour

I need to programmatically trigger the change event. This I can usually do, but I've found an unexplainable scenario which I hope you can help me with. I have this code:

jQuery(document).ready(function() {

    jQuery('.posts-list').hide();
    jQuery('.clinic').hide();

    jQuery('#category').change(function() {
        jQuery('.posts-list').hide();
        jQuery('#clinics-'+jQuery(this).val()).show().change();
    });

    jQuery('.posts-list').change(function()开发者_JAVA技巧 {
        jQuery('.clinic').hide();
        jQuery('#clinic-'+jQuery(this).val()).show();
    });

    jQuery('#category').change();
});

The important part of this code is the second to last line, and the function that's been bound to the change event of #category. The triggering of the change event inside that event actually works. But the triggering of the change event on the second to last line does not. If I put that into the console the first thing I do after having loaded the page, however, it works.

Anyone got an explanation?


jQuery('#category').trigger('change');

You actually want to trigger the event, rather than run the function. Let me know if this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜