的Javascript全局变量onsubmit事件处理程序不变
问题描述:
我已经附加onsubmit处理的形式标记是这样的:的Javascript全局变量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>
代码示例可能有用 – RaYell 2009-07-21 18:18:49