2013-03-21 34 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