我想阻止在这种情况下提交表单:
1)没有JQuery;
2)输入类型="提交" (而不是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;
}
}
答案 0 :(得分:0)
1。)在表单元素上添加onsubmit侦听器而不是输入元素
<form onsubmit="return top_controlla_ricerca();" 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 type="text" />
</fieldset>
</form>
2。)在此函数内手动删除提交(提交处理程序)
function top_controlla_ricerca() {
var nome_codice = document.getElementById("top_ric_strutt").value;
SOME_CODE
if (SOME_CONDITIONS) {
//document.getElementById("top_form_ricerca").submit(); <-- Remove this line
return true;
} else {
alert('MY_ALERT');
return false;
}
}