使用jquery检查表单的所有空字段

时间:2017-08-12 13:13:07

标签: jquery

大家好我是jquery的新手我希望在我提交表单时检查我的表单只是检查表单的所有字段,如果任何字段为空只显示错误,请填写所有字段 这是我的代码:

function validate() {
  //get all inputs of the form in array
  var inputs = $('#form :input');
  var k = 0;
  inputs.each(function() {
    if ($(this).val() == '') {
      k = 1;
    }
  });
  if (k == 1) {
    alert('please fill all fields');
  } else {
    alert('submitted successfully');
  }
}

function postvalue(id) {
  $('#value').val(id);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-horizontal" id="form">
  <fieldset>
    <!-- Form Name -->
    <legend>Form Name</legend>
    <div class="form-group">
      <input type="hidden" value="" id="value" />
      <label class="col-md-4 control-label">Double Button</label>
      <div class="col-md-8">
        <button type="button" onclick="postvalue(1)">click</button>
      </div>
    </div>
    <!-- Select Basic -->
    <div class="form-group">
      <label class="col-md-4 control-label" for="selectbasic">Select Basic</label>
      <div class="col-md-8">
        <select id="selectbasic" name="selectbasic" class="form-control valid">
      <option value="">select option</option>
      <option value="2">Option one</option>
    </select>
      </div>
    </div>
    <!-- Button (Double) -->
    <div class="form-group">
      <label class="col-md-4 control-label">Double Button</label>
      <div class="col-md-8">
        <input type="number" class="valid" />
      </div>
    </div>
    <input type="submit" value="submit" onclick="validate()" />
  </fieldset>
</form>

1 个答案:

答案 0 :(得分:1)

这很简单:

java.lang.NullPointerException
at $line926276415525.$read$$iwC$$iwC$$iwC$$iwC$$iwC$$$$3baf9f919752f0ab1f5a31ad94af9f4$$$$$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$anonfun$isinlist$1.apply(<console>:198)
at $line926276415525.$read$$iwC$$iwC$$iwC$$iwC$$iwC$$$$3baf9f919752f0ab1f5a31ad94af9f4$$$$$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$anonfun$isinlist$1.apply(<console>:184)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.apply(Unknown Source)
at org.apache.spark.sql.execution.Project$$anonfun$1$$anonfun$apply$1.apply(basicOperators.scala:51)
at org.apache.spark.sql.execution.Project$$anonfun$1$$anonfun$apply$1.apply(basicOperators.scala:49)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$$anon$10.next(Iterator.scala:312)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:103)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:47)
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:273)
at scala.collection.AbstractIterator.to(Iterator.scala:1157)
at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:265)
at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1157)
at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:252)
at scala.collection.AbstractIterator.toArray(Iterator.scala:1157)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$5.apply(SparkPlan.scala:212)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$5.apply(SparkPlan.scala:212)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)

这是有效的JsFiddle