1. 程式人生 > >為什麼使用jquery 的submit()方法不能提交表單?

為什麼使用jquery 的submit()方法不能提交表單?

今天遇到一個問題,就是用jquerr提交表單時出現問題 下面是程式碼

<form id="fm1" action="http://www.baidu.com" method="post">
<h2>請輸入您的使用者名稱和密碼.</h2>
<div><label for="username">使用者名稱:</label> <input id="username" name="username" type="text" value="" /></div>
<div><label for="password">密碼:</label> <input id="password" name="password" type="password" value="" /></div>
<div><label for="custom">自定義:</label> <input id="afterwardsVerify" name="afterwardsVerify" type="text" value="" /></div>
<div><input id="warn" name="warn" value="true" type="checkbox" /> <label for="warn">轉向其他站點前提示我。</label></div>
<div>
    <input name="submit" value="提交" type="button" onclick="attachedsign() />
 </div>
</form>

function attachedsign() 
 {
    var path = "/casPortal/login"; 
  
  $("#fm1).submit();
  }

上面程式碼看似沒問題,但是就是提交不了。於是上JQUERY API查詢原因:

Additional Notes:
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.

翻譯過來就是

其他注意事項:
表單和其子元素不宜用一個表單的屬性的屬性作為name或id的名稱,如submit, length, or method,是、會產生衝突。名稱衝突可能會導致混亂的失敗。對於一個完整的規則列表,並檢查這些問題標記。

於是上JQUERY API查詢原因,看到以下這段文字頓時明白了:

Additional Notes:
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.

其他注意事項:
表單和其子元素不宜用一個表單的屬性的屬性作為name或id的名稱,如submit, length, or method,是、會產生衝突。名稱衝突可能會導致混亂的失敗。對於一個完整的規則列表,並檢查這些問題標記。

原來就是這個name=”submit”的原因.........