我正在尝试调用我在标头中添加的 JS 函数。请在下面找到显示我的问题场景的代码。
注意:我无权访问我的应用程序中的正文。
每次我点击该元素时id="Save"
它只调用f1()
但不是fun()
。我怎样才能让它呼叫我的fun()
?
<!DOCTYPE html>
<html>
<head>
<script>
document.getElementById("Save").onclick = function fun()
{
alert("hello");
//validation code to see State field is mandatory.
}
function f1()
{
alert("f1 called");
//form validation that recalls the page showing with supplied inputs.
}
</script>
</head>
<body>
<form name="form1" id="form1" method="post">
State:
<select id="state ID">
<option></option>
<option value="ap">ap</option>
<option value="bp">bp</option>
</select>
</form>
<table><tr><td id="Save" onclick="f1()">click</td></tr></table>
</body>
</html>
您试图在加载元素之前附加事件侦听器函数。地方fun()
里面一个onload
事件监听函数。称呼f1()
在此函数内,作为onclick
属性将被忽略。
function f1() {
alert("f1 called");
//form validation that recalls the page showing with supplied inputs.
}
window.onload = function() {
document.getElementById("Save").onclick = function fun() {
alert("hello");
f1();
//validation code to see State field is mandatory.
}
}
JSFiddle
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)