2016-08-15 63 views
-2

我想防止保存按鈕上的默認操作,並且希望在檢查Ajax調用的結果後保存數據。這裏是我的腳本如何在Ajax中防止JQuery中的默認操作調用結果條件

var CreateUrl = '@Url.Action("Create", "PriMkt")'; 
$('#btnsave').click(function (e) { 
    // e.preventDefault(); 
    debugger; 
    var Amount = $("#TrasactionAmt").val(); 
    var CustomerId = $("#CUSTOMER_ID").val(); 
    $.ajax({ 
     url: '@Url.Action("CheckAvlBal", "PriMkt")', 
     type: "GET", 
     dataType: "JSON", 
     data: { Amount: Amount, CustomerId: $("#CUSTOMER_ID").val() }, 
     success: function (data) { 

      debugger; 
      if (data == 0) { 
       e.preventDefault(); 
       bootbox.alert("You have insufficient Balance", function() { 

        window.location.href = CreateUrl; 
       }); 
      } 
      else 

      { 

      } 
     } 
    }); 

}) 

這裏是我的控制器動作,我也運行完美的程序,它不是我的關注。

public int CheckAvlBal(int CustomerId, decimal Amount) 
    { 


     var Acct_Desc = new System.Data.Objects.ObjectParameter("p_ACCT_DESC", typeof(string)); 
     var Acct_CCy = new System.Data.Objects.ObjectParameter("aCCT_CCY", typeof(string)); 
     var P_Branch = new System.Data.Objects.ObjectParameter("p_BRANCH", typeof(string)); 
     var Acc_Type = new System.Data.Objects.ObjectParameter("p_ACCT_TYPE", typeof(string)); 
     var Deposit_Type = new System.Data.Objects.ObjectParameter("p_DEPOSIT_TYPE", typeof(string)); 
     var Client_No = new System.Data.Objects.ObjectParameter("p_CLIENT_NO", typeof(int)); 
     var Ownership_Type = new System.Data.Objects.ObjectParameter("p_OWNERSHIP_TYPE", typeof(string)); 
     var Profit_Centre = new System.Data.Objects.ObjectParameter("p_PROFIT_CENTRE", typeof(string)); 
     var Acct_OpenDate = new System.Data.Objects.ObjectParameter("p_ACCT_OPEN_DATE", typeof(string)); 
     var Acct_Status = new System.Data.Objects.ObjectParameter("p_ACCT_STATUS", typeof(string)); 
     var Ledger_Bal = new System.Data.Objects.ObjectParameter("p_LEDGER_BAL", typeof(decimal)); 
     var Actual_Balance = new System.Data.Objects.ObjectParameter("p_ACTUAL_BAL", typeof(decimal)); 
     var Daily_Avg_Balance = new System.Data.Objects.ObjectParameter("p_AVG_DAILY_BAL", typeof(decimal)); 
     var Acct_Available_Balance = new System.Data.Objects.ObjectParameter("p_ACCT_AVAIL_BAL", typeof(decimal)); 
     var Last_Bal_Update = new System.Data.Objects.ObjectParameter("p_LAST_BAL_UPDATE", typeof(string)); 

     // string p_ACCT_NO = "187201016446"; 
     CUSTOMER customer = db.CUSTOMERs.Find(CustomerId); 
     string p_ACCT_NO = customer.PKR_ACCNO; 

     db.GET_ACCT_DETAILS(p_ACCT_NO, Acct_Desc, Acct_CCy, P_Branch, Acc_Type, Deposit_Type, Client_No, Ownership_Type, Profit_Centre, Acct_OpenDate, Acct_Status, Ledger_Bal, Actual_Balance, Daily_Avg_Balance, Acct_Available_Balance, Last_Bal_Update); 

     var AvlAmount =Acct_Available_Balance.Value; 

     if (Amount < Convert.ToDecimal(AvlAmount)) 
     { 
      return 1; 
     } 
     else 
     { 
      return 0; 
     } 
    } 
+0

'ajax'是ansyncron,這是行不通的方式。您應該對兩種狀態使用回調函數,並完全忽略默認按鈕'click'。 – eisbehr

回答

0

您需要的邏輯是提交/保存,如果有效的話,不阻止提交如果無效。

我假設按鈕#btnsave的形式爲一個提交按鈕,這樣你就可以做這樣的:

var CreateUrl = '@Url.Action("Create", "PriMkt")'; 
$('#btnsave').click(function (e) { 
    e.preventDefault(); 
    debugger; 
    var Amount = $("#TrasactionAmt").val(); 
    var CustomerId = $("#CUSTOMER_ID").val(); 
    $.ajax({ 
     url: '@Url.Action("CheckAvlBal", "PriMkt")', 
     type: "GET", 
     dataType: "JSON", 
     data: { Amount: Amount, CustomerId: $("#CUSTOMER_ID").val() }, 
     success: function (data) { 

      debugger; 
      if (data == 0) { 
       bootbox.alert("You have insufficient Balance", function() { 

        window.location.href = CreateUrl; 
       }); 
      } else { 
       // Replace with form id 
       $('#form').submit(); 
      } 
     } 
    }); 

}) 
相關問題