2013-03-21 37 views
0

我正在處理一個項目,我有一些錯誤檢查。但是,每次都要提交表單,所以我不得不中斷提交。這是我做的。一旦錯誤檢查破壞了表單就不會提交

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "order-form" })) 
{ 
... 

<input type="submit" name="[email protected](item.Id)" value="@T("Admin.Common.Save")" id="[email protected](item.Id)" class="adminButton" style="display: none;" onclick="SaveBtn(@item.Id);" /> 

... 

    var originalIssuedQty = 0; 

    function SaveBtn(id) { 
    var quantity = parseInt($("#pvQuantity" + id).val()); 
    var issuedQty = parseInt($("#pvIssuedQty" + id).val()); 
    var stockQty = parseInt($("#hfStockQty" + id).val()); 
    var availableStockQty = stockQty + parseInt(originalIssuedQty); 

    //Issued Quantity cannot exceed Quantity (you can't issue more than requested) 
    if (issuedQty > quantity) { 
    alert("Issued Quantity cannot exceed Quantity."); 
    $("#order-form").submit(function (e) { e.preventDefault(); }); 
    return false; 
    } 

    //Make sure Issued Quantity is within Available Stock Quantity 
    if (issuedQty > availableStockQty) { 
    alert("There is not enough Products in Stock to issue this amount."); 
    $("#order-form").submit(function (e) { e.preventDefault(); }); 
    return false; 
    } 

    //Present confirmation 
    var result = confirm('@T("Admin.Common.AreYouSure")'); 
    if (!result) { 
    $("#order-form").submit(function (e) { e.preventDefault(); }); 
    return false; 
    } 
    else { 
    $("#order-form").submit(function (e) { this.submit(); }); 
    //$("#order-form").submit(function (e) { return true; }); 
    } 
    }  
    ... 
} 

這是問題所在。每當我嘗試提交第一次,而沒有觸發我的錯誤檢查,事情就會起作用。當我觸發錯誤檢查時,事情就會起作用。但是,如果我修復錯誤並嘗試再次提交,則頁面僅會刷新。任何想法都是非常有幫助的。謝謝。

+0

您是否嘗試過使用Chrome開發人員工具或firefox上的firefox進行調試? – 2013-03-21 19:14:25

+0

向我們顯示您的表格html – Huangism 2013-03-21 19:23:38

+0

我很希望我可以Fabio。不幸的是,由於我爲之工作,我們只能使用IE8糟糕的開發工具。沒有錯誤從我所見過的。 – IyaTaisho 2013-03-21 19:28:48

回答

2

你讓事情太複雜了。

這是你如何做驗證和如何從當它是不是有效的提交停止形式的基本模板:

$(function() { 
    $("#order-form").submit(function (e) { 
    var isValid = false; 

    // Do your validation here and put the result in the variable isValid 

    if (!isValid) { 
     e.preventDefault(); // If the form is invalid stop the submit, otherwise proceed 
    } 
    }); 
}); 
+0

完美!謝謝您的幫助。 – IyaTaisho 2013-03-21 19:55:34

2

每次調用$("#order-form").submit(function (e) { whatever });時,你增加一個附加處理函數。它不會刪除您已添加的處理程序。這可能是它破裂的原因。

反覆更改submit事件處理程序是一個混亂的方式來做到這一點。相反,您應該有一個處理提交事件的函數,並且該函數應該執行(或調用)錯誤檢查,並且如果需要的話(如ZippyV正在建議)。

+0

感謝您的幫助。在我讀完你所擁有的東西后,我完全明白了。再次感謝。 – IyaTaisho 2013-03-21 19:56:01