Javascript无法正常工作

时间:2015-03-07 09:16:53

标签: javascript c# jquery asp.net

我有一个类似下面的javascript代码,要求检查至少一个复选框,并确认我是否要删除数据。但即使在选择NO之后,它也会删除数据:

请参阅代码: -

function ValidateAll() {
        var chkselectcount = 0;
        var gridview = document.getElementById('<%= grdTeacherSalary.ClientID %>');
        for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
            var node = gridview.getElementsByTagName("input")[i];

            if (node != null && node.type == "checkbox" && node.checked) {
                chkselectcount = chkselectcount + 1;
            }
        }
        if (chkselectcount == 0) {
            alert("Please select atleast One CheckBox");
            return false;
        }
        else {
            ConfirmationBox();
        }
    }
    function ConfirmationBox() {
        var result = confirm("Are you sure, you want to delete the Users ?");
        if (result) {
            return true;
        }
        else {
            return false;
        }
    }

更新HTML: -

<asp:Button ID="btnDelete" runat="server" CausesValidation="false" CssClass="btn btn-danger" Text="Delete" OnClick="btnDelete_Click" OnClientClick="javascript:return ValidateAll();" />

任何帮助..!

1 个答案:

答案 0 :(得分:2)

只需将您的代码更新为

function ValidateAll() {
        var chkselectcount = 0;
        var gridview = document.getElementById('<%= grdTeacherSalary.ClientID %>');
        for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
            var node = gridview.getElementsByTagName("input")[i];

            if (node != null && node.type == "checkbox" && node.checked) {
                chkselectcount = chkselectcount + 1;
            }
        }
        if (chkselectcount == 0) {
            alert("Please select atleast One CheckBox");
            return false;
        }
        else {
           //Instead of ConfirmationBox();
          return confirm("Are you sure, you want to delete the Users ?");
          // Or
          return ConfirmationBox();
        }
    }