我在这样的表单标记中附加了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>
结果 - 在窗口中我有一个新标题,但是警告框显示旧的变量值。
答案 0 :(得分:1)
回答上一个问题,不,从标记/属性调用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
似乎不会更改。< / p>
<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>