[英]Scope of Submit Button and calling a function
The code I'm working with is quite long, so I will paraphrase my problem. 我正在使用的代码很长,所以我将解释我的问题。
The code I'm working with looks (roughly) like this: 我正在使用的代码看起来(大致)如下:
<html><HEAD><meta http-equiv='Cache-control' content='no-cache'><meta http- equiv='Expires' content='-1'>
<script>
function sendRequestXML()
{
function asgn()
{
alert("!");
}
document.write("<input type='submit' value='Submit Survey' onclick='asgn()' />
</script>
<body onload="sendRequestXML()">
<h4><u>EAM fetching data....</u></h4>
</body>
</html>
The issue I'm having is that I cannot call the asgn function with the button onclick. 我遇到的问题是我无法使用onclick按钮调用asgn函数。 I think this has something to do with it being out of scope for the button.
我认为这与按钮超出范围有关。 The error I keep getting is that "asgn is undefined or not a function object".
我不断收到的错误是“ asgn未定义或不是函数对象”。 How can I make the button and the function in the same scope?
如何使按钮和功能在同一范围内?
Try: 尝试:
<script>
window.asgn = function() {
alert("!")
}
</script>
This will make the function available globally. 这将使该功能全局可用。
A better alternative (as it does not need 'onclick=...') would be: 更好的选择(因为它不需要'onclick = ...')是:
<script>
document.querySelector("submit").addEventListener("click", function() {
alert("!")
});
</script>
I think your code should be like this for button to work 我认为您的代码应该像这样使按钮起作用
<html><HEAD><meta http-equiv='Cache-control' content='no-cache'><meta http- equiv='Expires' content='-1'>
<script>
function sendRequestXML()
{
document.write("<input type='submit' value='Submit Survey' onclick='asgn()' />");
}
function asgn()
{
alert("!");
}
</script>
<body onload="sendRequestXML()">
<h4><u>EAM fetching data....</u></h4>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.