2009-07-21 102 views
0

我已經附加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> 

結果 - 在窗口,我有一個新的冠軍,但警告框顯示舊變量值。

+2

代碼示例可能有用 – RaYell 2009-07-21 18:18:49

回答

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不會出現改變。

<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> 
相關問題