簡體   English   中英

返回false不適用於jQuery中的表單提交

[英]return false not working on form submit in jquery

 function SaleProduct() { var CusId=$('#CusId').val(); $.get('customer-id.php?CusId='+CusId, function(data) { if(data==0){ alert("Customer Id not valid.") return false; } }); } 
 <form action="sales-edit-insert.php" method="post" onSubmit="return SaleProduct()"> <input type="text" name="CusId" id="CusId"/> <!--Some input field here--> <input type="submit" value="Submit"/> </form> 

返回false; 或e.preventDefault(); 提交表單時無法使用上述功能。 顯示警報后提交表單。

您的SaleProduct返回任何內容(實際上是undefined )。
您可以停止立即發送表單, return false; onsubmit屬性內部:

<form action="sales-edit-insert.php" method="post" onSubmit="SaleProduct(); return false;">

之后,您可以手動提交表單:

function SaleProduct() {   
  var form = ...;
  var CusId=$('#CusId').val(); 
  $.get('customer-id.php?CusId='+CusId, function(data) {
    if(data==0){
      alert("Customer Id not valid.")
      return;
    }
    form.submit();
  });
  return false; // you can also move this statement to here from attribute
}

獲取表單元素的最簡單方法是將其提供給onsubmit

<form action="sales-edit-insert.php" method="post" onSubmit="return checkCustomer(this)">

和js:

function checkCustomer(form) {
  //code from above
  return false;
}

您已經在使用jQuery,那么為什么還要麻煩onsubmit屬性。 嘗試

<form action="sales-edit-insert.php" method="post" id="sale-product-form">

jQuery(function($) {
  $('#sale-product-form').on('submit', function(e) {
    e.preventDefault()
    var form = this;
    $.get('customer-id.php', $(form).serialize(), function(data) {
      if (data == 0) {
        alert('Customer Id not valid.')
      } else {
        form.submit() // submit normally
      }
    })
  })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM