javascript 强制运行 .change() 函数 - jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3808185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:19:46  来源:igfitidea点击:

Force .change() function to run - jQuery

javascriptjquerydrop-down-menu

提问by Bill Addison

I have a .change() function set for a dropdown menu with an ID of #country. When the page loads, I'm trying to set the dropdown menu to "United States" and run the .change() function:

我为 ID 为 #country 的下拉菜单设置了 .change() 函数。当页面加载时,我试图将下拉菜单设置为“美国”并运行 .change() 函数:

$('#country').change(function () {
    resetDisclosure();
    var countryCode = $(this).val();
    var countryName = $('#country option:selected').text();

    $('#'+countryCode.toString()).fadeIn('slow');

    if(countryCode == 'OC' || countryCode == 'EU') {
        $('#OC h4, #EU h4').html('For Residents of <strong>' + countryName + '</strong>');
    }

    $.fancybox.resize();
    $.fancybox.center();
});

$("#country").val('OC');
$("#country").change();

The last function there is wrong, because I can't force the .change() on load. How can I go about forcing the change function?

最后一个函数是错误的,因为我不能强制加载 .change() 。我该如何去强制更改功能?

I'm super beginner and have tried to assign the contents of the .change() function to a different function and call that, but it didn't work either.

我是超级初学者,并尝试将 .change() 函数的内容分配给不同的函数并调用它,但它也不起作用。

回答by prodigitalson

Try:

尝试:

$("#country").val('OC').trigger('change');

$("#country").val('OC').trigger('change');