【发布时间】:2016-04-12 20:32:33
【问题描述】:
我想在这种情况下阻止提交表单:
1) 没有 JQuery;
2) 使用输入 type="submit"(而不是 type="button"),因为这是(我认为)使用 Enter 键提交表单的唯一方法
这是我的代码:
<form name="form_ricerca" action="trova_hotels.htm" id="top_form_ricerca" method="post">
<fieldset style="margin: 10px;text-align: center;">
<input id="top_ric_strutt" type="text" name="ricerca" size="40"
<input onclick="return top_controlla_ricerca();" >
</fieldset>
</form>
这就是函数:
function top_controlla_ricerca() {
var nome_codice = document.getElementById("top_ric_strutt").value;
SOME_CODE
if (SOME_CONDITIONS) {
document.getElementById("top_form_ricerca").submit();
return true;
} else {
alert('MY_ALERT');
return false;
}
}
【问题讨论】:
-
而不是使用
onclick的按钮使用onsubmit形式的事件-<form name="form_ricerca" action="trova_hotels.htm" id="top_form_ricerca" method="post" onsubmit="return top_controlla_ricerca();">然后<input type="submit" value="Save" >
标签: javascript html forms button submit