是否可以在此javascript中添加取消按钮,以便不提交表单? 此Javascript目前正在验证这些表单字段,但如果由于某种原因我输入了错误信息,则我无法取消并阻止提交表单。
<form METHOD="POST" ACTION="delformaction.cfm" name="deleteform" onSubmit="return validate(deleteform);">
<script language="JavaScript">
function validate(form)
{
if (form.Institution.selectedIndex == 0)
{
alert("You must select an institution for any necessary future correspondence.");
return false;
}
if (form.Requestors_name.value == "")
{
alert("You must enter your name for any necessary future correspondence.");
return false;
}
if (form.EntityFirstName.value == "")
{
alert("You must enter the Entity's first name.");
return false;
}
alert("Your form has been successfully submitted!");
// form.reset();
return true;
}
</script>
<input type="submit" validate="submitonce" name="DeleteEntity" value="SUBMIT ONCE" class="SubmitButtons">
答案 0 :(得分:0)
您可以使用.preventDefault()
取消默认操作。即。表格提交。
See this StackOverflow question.
<form METHOD="POST" ACTION="delformaction.cfm" name="deleteform" onSubmit="return validate(event, deleteform);">
<script language="JavaScript">
function validate(e, form)
{
var fail = false
if (form.Institution.selectedIndex == 0)
{
alert("You must select an institution for any necessary future correspondence.");
fail = true;
}
if (form.Requestors_name.value == "")
{
alert("You must enter your name for any necessary future correspondence.");
fail = true;
}
if (form.EntityFirstName.value == "")
{
alert("You must enter the Entity's first name.");
fail = true;
}
if (fail) {
e.preventDefault();
return false;
}
alert("Your form has been successfully submitted!");
// form.reset();
return true;
}
</script>
<input type="submit" validate="submitonce" name="DeleteEntity" value="SUBMIT ONCE" class="SubmitButtons">