從submition預防形式,我有以下形式:上自動提示
<form action="/web/projects/add" id="EventAddForm" method="post" accept-charset="utf-8">
<div style="display:none;"><input type="hidden" name="_method" value="POST"/></div>
<div class="input text">
<label for="AddressSearch">Search address</label>
<input name="adress_search" type="text" id="AddressSearch"/>
</div>
<div class="input text required">
<label for="EventAddress">Address</label>
<input name="data[Event][address]" type="text" id="EventAddress"/>
</div>
<div class="input text required">
<label for="EventPostNr">Post nr</label>
<input name="data[Event][post_nr]" type="text" id="EventPostNr"/>
</div>
<div class="input text required">
<label for="EventCity">City</label>
<input name="data[Event][city]" type="text" id="EventCity"/>
</div>
<div class="submit">
<input type="submit" value="Save"/>
</div>
</form>
前面提到的字段用於通過谷歌地圖選擇autosuggested地址,我省略了更好的可讀性的代碼,但問題出現時,出現自動提示下拉菜單。如果用戶通過鍵盤導航到其中一個建議並按enter
,則會提交整個表單。我有以下功能,防止形式從submition鼠標點擊:
jQuery('#EventAddForm').submit(function(event) {
if ($("#AddressSearch").is(":focus") ||
$("#EventPostNr").is(":focus") ||
$("#EventCity").is(":focus"))
event.preventDefault();
});
但我想要做的是防止對enter
點擊的形式爲好。
我缺乏一些jQuery的知識,只是無法讓它運行,所以任何幫助或指導都非常感謝。
剛剛嘗試過,但表單仍在提交中。 – Domas
這可以單獨使用(請參閱jsfiddle)。你有沒有看過你的控制檯來檢查沒有什麼能夠阻止你的JS工作? – Popnoodles