在不使用 JQuery 且输入类型="submit" 的情况下阻止提交表单
Block submit of a form without using JQuery and with input type="submit"
我想在这种情况下阻止提交表单:
1) 没有 JQuery;
2) 输入 type="submit"(而不是 type="button"),因为这是(我认为)使用 Enter Key
提交表单的唯一方法
这是我的代码:
<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;
}
}
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;
}
}
我想在这种情况下阻止提交表单:
1) 没有 JQuery;
2) 输入 type="submit"(而不是 type="button"),因为这是(我认为)使用 Enter Key
提交表单的唯一方法这是我的代码:
<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;
}
}
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;
}
}