javascript 调用 onchange 处理函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7620041/
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-26 00:43:22  来源:igfitidea点击:

Call onchange handler function

jqueryjavascript

提问by

I have this OnChangefunction:

我有这个OnChange功能:

$("select[id^=type]").change(function(){/*...*/});

The question is: How can I call this from the following function:

问题是:我如何从以下函数调用它:

$("#all_db").change(function()
{
    /*...*/
    $("select[id^=type]").each.trigger("change"); //I have tried this
});

采纳答案by karim79

$("select[id^='type']").change();

is all you need. I would quote the value you pass to the startsWithselector.

是你所需要的全部。我会引用您传递给startsWith选择器的值。

回答by Chubs

You have to declare the function separately. In your example you are saying to JQuery to trigger the function change when it is the time. Nevertheless, such a function doesn't exist. To do it try to declare your function as a separate one. Like this:

您必须单独声明该函数。在您的示例中,您对 JQuery 说在时机成熟时触发函数更改。然而,这样的功能并不存在。为此,请尝试将您的函数声明为一个单独的函数。像这样:

function myDesiredFunc(){ /*console.log("Inside it");*/ }
$("#all_db").change(myDesiredFunc);
$("select[id^=type]").change(myDesiredFunc);

Note that I commented console.log because this will only work if you have firebug installed.

请注意,我对 console.log 进行了评论,因为这仅在您安装了 firebug 时才有效。

I hope it helps.

我希望它有帮助。

回答by Joshua G

$('select').change();then you can do $('select[id='yourIdHere']').change();or whatever you want.

$('select').change();然后你可以做$('select[id='yourIdHere']').change();任何你想做的事。

回答by A.Quiroga

See this:

看到这个

$("#all_db").change(function()
{
    /*...*/
    $("select[id^=type]").change();
});

Usually in Jquery , the same name to create a trigger , runs to fire it.

通常在 Jquery 中,同名创建触发器,运行以触发它。

回答by usoban

$("select[id^=type]").trigger("change");

$("select[id^=type]").trigger("change");

should do the trick. Otherwise, that one should:

应该做的伎俩。否则,那个人应该:

$("select[id^=type]").each(function(i){
 $(this).trigger("change");
});