我正在使用multiple-select.js,当我使用 setSelects 方法设置值时,它们会设置值并动态触发onchange函数。但在我的情况下,我想禁用动态触发。此外,我无法修改multiple-select.js,因为许多人在不同的模块中使用此插件。
如果我能检测到这个onchange函数是从动态触发器或本机下拉列表更改调用,我的问题将解决。但是不要使用任何全局变量。只有我们可以使用参数(可能我们可以使用onchange =" ddlchange(this)")。
我写了一个简单的例子,比如我的问题
<script>
function check(ths)
{
//Here I want to know is it come from real change or manually trigger, without using any variable
alert("function called");
}
function trifun()
{
//I can't modify this line, because it is in external plugin
document.getElementById('ddl').onchange();
}
</script>
<button onclick="trifun();">Trigger</button>
<select id="ddl" onchange="check(this);">
<option value="1">Test</option>
<option value="2">Test</option>
<option value="3">Test</option>
<option value="4">Test</option>
<option value="5">Test</option>
</select>
&#13;
答案 0 :(得分:0)
这有助于你:
<html>
<head>
<title></title>
</head>
<body>
<script>
var trg = document.getElementById("ddl");
function check(ths,event)
{
//Here I want to know is it come from real change or manually trigger, without using any variable
if (event.type == "change")
alert("function called with Change");
else
alert("function called with click");
}
function trifun(event)
{
//I can't modify this line, because it is in external plugin
document.getElementById('ddl').onchange(event);
}
</script>
<button onclick="trifun(event);">Trigger</button>
<select id="ddl" onchange="check(this,event);">
<option value="1">Test</option>
<option value="2">Test</option>
<option value="3">Test</option>
<option value="4">Test</option>
<option value="5">Test</option>
</select>
</body>
</html>