[英]Javascript globals variables unchangeable in onsubmit event handler
I've attached onsubmit handler in a form tag like this: 我已经将onsubmit处理程序附加在这样的表单标签中:
<form action="file.php" method="post" onsubmit=" return get_prepared_build(this);" >
but whatever global variable (defined earlier, of course) I try to change inside get_prepared_build() function - later it apprears non-modified. 但是无论使用什么全局变量(当然,是在前面定义的),我都尝试在get_prepared_build()函数内部进行更改-稍后它会不作修改。 Looks like that this function deals with a local copy of everything, even document property values are not saved.
看起来此函数处理了所有内容的本地副本,即使未保存文档属性值也是如此。
Are there scope/visibility problems when javascript functions are called this way from tags/attributes? 从标签/属性以这种方式调用javascript函数时,是否存在范围/可见性问题?
Here is the function: 这是函数:
function give_link(results)
{
document.title = 'visibility test';
return false;
}
then below in the document I have 然后在文件下面
<script>alert('test' + document.title );</script>
As a result - in the window I have a new title, but the alert box displays old variable value. 结果-在窗口中,我有一个新标题,但警报框显示了旧的变量值。
To answer your last question, no, there are no scope/visibility problems when JavaScript functions are called from tags/attributes: 要回答您的最后一个问题,不,从标记/属性调用JavaScript函数时,没有范围/可见性问题:
<script type="text/javascript">
var x = 'Hello';
function get_prepared_build(f) {
alert('Start get_prepared_build: x=' + x + '; document.cookie=' + document.cookie);
x = 'There';
// deliberately invalid cookie for test purposes only
document.cookie = 'test';
alert('End get_prepared_build: x=' + x + '; document.cookie=' + document.cookie);
return false;
}
</script>
<form action="file.php" method="post" onsubmit="var ret=get_prepared_build(this);alert('Outside get_prepared_build: x=' + x + '; document.cookie=' + document.cookie);return ret;">
<input type="submit">
</form>
As was mentioned in comments, a sample of code demonstrating your specific problem would be helpful. 正如评论中提到的,演示您的特定问题的代码示例将很有帮助。
EDIT: In your example, the function that updates document.title
is never invoked, or is invoked after you alert()
the current value, so document.title
doesn't appear to change. 编辑:在您的示例,更新
document.title
的函数永远不会调用,或在您alert()
当前值后调用,因此document.title
似乎没有改变。
<script type="text/javascript">
function changeDocumentTitle(f) {
// this only runs onsubmit, so any alert()s at the bottom
// of the page will show the original value before the
// onsubmit handler fires
document.title = 'new value';
return false;
}
</script>
<form onsubmit="return changeDocumentTitle(this);">
<input type="submit">
</form>
<script type="text/javascript">
// this executes when the page loads, so it will show
// the value before any onsubmit events on the page fire
alert(document.title); // original value
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.