简体   繁体   English

如何防止自动提交表格

[英]How to prevent auto form submit

I have the following input box which takes input from barcode scanner. 我有以下输入框,用于接收条形码扫描仪的输入。

 <form class="m-form m-form--fit m-form--label-align-right" method="post" action="{{ url('updateInvoice') }}" id="invoice_update">
    <div class="form-group m-form__group">
       <label for="exampleInputEmail1">Search by item name or barcode</label>
       <input type="text"  autofocus class="form-control m-input" id="productSearch"  placeholder="Item name">              
    </div>
     <button type="submit" name="pdf" class="btn btn-success">Update & print
     </button>
</form>

After getting the input from barcode it does following operation (from the input it checks value from database and add to row) 从条形码获取输入后,将执行以下操作(从输入中,它检查数据库中的值并添加到行中)

$( "#productSearch" ).change(function(event) {
    event.preventDefault();

  $.ajax({
          type: "get",
          context: this,
          url: "{!! asset('searchByProductName') !!}",
          dataType: 'json',
          data: { name:this.value },
          success: function(response)
            {
              if ($('#' + response.id).length !== 0)

              { $(this).val("").focus(); return false; }

             var markup = "<tr id="+response.id+"><input type='hidden' name='product_id[]'  value="+response.id+"><td><i class='flaticon-delete-1 delete-row' onclick='deleteRow(this)'></i></td><td>"+response.product_name+"</td><td>"+response.product_unit_price+"</td><td><input type='text' name='quantity[]' class='quantity' value='1'></td><td class='total'>"+response.product_unit_price+"</td><td>"+response.notes+"</td></tr>";  
              $("table tbody").append(markup); 
              $(this).val("").focus(); return false; 
              } 

        });

});

But the problem the form get auto submit ie, i can't add more than one value in the table. 但是表格自动提交的问题,即我不能在表中添加多个值。 How do i prevent the form automatic submit so that more that one input can be taken with the above ajax code? 我如何防止表单自动提交,以便以上ajax代码可以进行更多输入?

I'm not sure if the barcode scanner put the "enter key", but how about this one? 我不确定条形码扫描仪是否输入了“输入密钥”,但是这个怎么样?

$("#form-id").on("submit", function(e) {
    if ($("#input-id(productSearch)").is(":focus")) {
        e.preventDefault();
    }
});

IMO, the easiest way is to add a hidden input IMO,最简单的方法是添加隐藏的输入

<input type="hidden" />

browser will auto submit if there is one input in a form. 如果表单中只有一个输入,浏览器将自动提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM