我們使用自定義按鈕來提交表單。點擊事件被分配給.btn-submit's以提交相應的表單。但是,如果<button type="submit">
或<input type="submit">
元素位於<form>
之內,則在用戶名或密碼文本字段中按ENTER鍵不會提交表單。當沒有type =「submit」元素時,表單不會在ENTER上提交
如何在<form>
中沒有type="submit"
元素的input
字段中按ENTER鍵時仍允許用戶提交表單?
HTML
<form action="post.php" method="post">
<p><b>Username</b> <input type="text" name="username" style="width: 300px; font-size: 18px"></p>
<p><b>Password</b> <input type="password" name="password" style="width: 300px; font-size: 18px"></p>
<input type="hidden" name="user_login" value="1">
<div class="btn-inline btn-green btn-submit">Log In</div>
</form>
JS
$(document).ready(function()
{
$('.btn-submit').on('click', function(e)
{
$(this).parents('form:first').submit();
});
});
您需要捕獲輸入按鍵,最好在窗體上使用jQuery委託。 – Jason 2014-10-17 21:30:21
[按提交按鈕提交表單而不提交按鈕]可能的重複(http://stackoverflow.com/questions/477691/submitting-a-form-by-pressing-enter-without-a-submit-button) – CBroe 2014-10-17 21:31:12
爲什麼你不想添加一個按鈕(type = submit)?你可以通過CSS隱藏它:'display:none' – timaschew 2014-10-17 21:38:03