2013-03-12 5 views
0

我希望我可以在這個正確解釋...的Javascript:嘗試訪問形式圓括號,而不是方括號屬性在IE

以下網頁包含有onsubmit處理的形式。 onSubmit處理程序是一個總是返回false的函數,意思是表單不應該提交。但是這隻適用於IE8(也可能適用於其他版本的IE)。

爲了得到這個在火狐和Chrome的工作我必須警告更改爲:

alert(foo["firstName"].value); 

我明白了爲什麼用方括號是正確的形式,形式是一個關聯數組。

  • 但爲什麼表單在FF和Chrome中提交(如form("firstName")顯然是錯誤的)?
  • 有沒有我能捕捉到的錯誤? (我在Firebug中看不到任何明顯的東西。)
  • 有沒有辦法確保表單僅在返回時返回true

感謝

<html> 
<head> 
<script> 
function alwaysReturnFalse() { 
    alert(foo("firstName").value); 
    return false; 
} 
</script> 
</head> 
<body> 
<form name="foo" onSubmit="return alwaysReturnFalse();"> 
    First name: <input name="firstName" type="text" value="Bob"/><br/> 
    Last name: <input name="lastName" type="text"/><br/> 
    <input type="submit" value="Submit"/> 
</form> 
This form has an onSubmit handler that always returns false - meaning the form should never submit.<br/> 
It works in IE8 - e.g. the onSubmit handler executes an alert() and then returns false.<br/> 
It fails in Firefox and Chrome - e.g. the form is submitted. 
</body> 
</html> 

回答

2

的表單提交,因爲其他瀏覽器拋出一個錯誤。錯誤導致函數提前退出。結果,代碼不會返回false,因此表單提交。

+1

你知道錯誤具體是什麼嗎? – 2013-03-12 17:59:06

+1

@REsmond錯誤是'foo()',錯誤信息應該是'TypeError:object is not a function' – epascarello 2013-03-12 18:02:09

相關問題