无法阻止表单提交空输入

时间:2017-08-26 23:16:01

标签: javascript html forms submit

当任何输入为空时,我无法阻止表单提交。它没有错误,但它也没有停止提交。我在表单提交输入中调用了该函数。它位于onClick调用之下。

JS档案

$state

HTML文件

TX

2 个答案:

答案 0 :(得分:2)

修改

使用2017-08-26 20:18:52.006 ERROR 2784 --- [nio-8080-exec-4] org.hibernate.AssertionFailure : HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException 2017-08-26 20:18:52.009 WARN 2784 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction 属性,您甚至不需要任何JavaScript。请参阅演示2.有关正常运行的演示,请参阅此PLUNKER

<小时/>

OLD

在每个required添加return false

之前

演示(由于SO安全措施而无法运行)

&#13;
&#13;
e.preventDefault()
&#13;
function stopSubmit(e) {
  var inDay = document.getElementById(indate).value;
  var inType = document.getElementById(intype).value;
  var inAmount = document.getElementById(inamount).value;
  if (inDay == "") {
    alert("Please select a date");
    e.preventDefault();
    return false;
  }
  if (inType == "Select One") {
    alert("Please select a frequency");
    e.preventDefault();
    return false;
  }
  if (inAmount == "") {
    alert("Please enter an amount");
    e.preventDefault();
    return false;
  } else {
    alert("Your form was submitted");
  }
}
&#13;
&#13;
&#13;

演示2使用必需属性

&#13;
&#13;
<form>

  <td>
    <input type="submit" name="submitincome" value="submit" onclick="stopSubmit()">
  </td>

</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我能够看到你在哪里犯了错误,document.getElementById()接受一个字符串作为参数,但你碰巧传递了一个未定义的变量

function stopSubmit(){
  var inDay = document.getElementById('indate').value;
  var inType = document.getElementById('intype').value;
  var inAmount = document.getElementById('inamount').value;
  if (inDay === "") {
  alert("Please select a date");
  return false;
}
if (inType == "Select One"){
  alert("Please select a frequency");
  return false;
}
if (inAmount === ""){
  alert("Please enter an amount");
  return false;
}
else {
  alert("Your form was submitted");
}
}