[英]Javascript globals variables unchangeable in onsubmit event handler
我已经将onsubmit处理程序附加在这样的表单标签中:
<form action="file.php" method="post" onsubmit=" return get_prepared_build(this);" >
但是无论使用什么全局变量(当然,是在前面定义的),我都尝试在get_prepared_build()函数内部进行更改-稍后它会不作修改。 看起来此函数处理了所有内容的本地副本,即使未保存文档属性值也是如此。
从标签/属性以这种方式调用javascript函数时,是否存在范围/可见性问题?
这是函数:
function give_link(results)
{
document.title = 'visibility test';
return false;
}
然后在文件下面
<script>alert('test' + document.title );</script>
结果-在窗口中,我有一个新标题,但警报框显示了旧的变量值。
要回答您的最后一个问题,不,从标记/属性调用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>
正如评论中提到的,演示您的特定问题的代码示例将很有帮助。
编辑:在您的示例,更新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.