如何修正错误的表单提交

时间:2019-05-28 18:56:49

标签: jquery submit

点击提交按钮,它甚至提交几乎为空的表格。这是我在Codepen上的完整代码:https://codepen.io/cfrank2000/pen/LoBvPd

<div class="form-group" >
    <span class="glyphicon glyphicon-star"></span>
         <div class="input-display">
           <label for="first-name">  First Name:</label>
              <input type="text" class="form-control input-check" id="first-name" placeholder="Enter your First name" name="First-Name">
             <span class="fielderror" >Please enter your first name.</span>
         </div>
</div>
<div class="form-group">
  <span class="glyphicon glyphicon-star"></span>
  <div class="input-display">
      <label for="last-name">  Last Name:</label>
      <input type="text" class="form-control input-check" id="last-name" placeholder="Enter your Last name" name="Last-Name">
  <span class="fielderror">Please enter your last or family name.</span>
</div>

$(".button1").click(function(){
var emptyfield = true;
   $(".input-check").each(function(){

     //$("span").text(inpattr)
    var inputattr = $(this).attr("id");  
     if ( $(this).val()==="") {
        inputmsg(this, inputattr);
          //$(this).next(".fielderror").html(inpattr+" is EMPTY: Please enter data into this input");
          //$(this).next(".fielderror").addClass('warning').removeClass('ok');
          //$("form").submit(function(e){ });
                       emptyfield = false;       

      } else {
        //inputmsg(this);
        $(this).next('span').text(inpattr + " " + " is Ok");
      }
      //alert(inpattr);

     });
     if ( emptyfield === false ) {
        event.preventDefault();
     }
});

如果我尝试提交空白表单,则不会,但会收到错误消息。如果我部分填写了表单,则按“提交”按钮将提交一半的空白表单,这是错误的,我不理解为什么。

0 个答案:

没有答案