Javascript未按要求运行

Javascript not working as Required

本文关键字:运行 Javascript      更新时间:2023-09-26

我有一个如下的javascript代码,要求至少选中一个复选框,并确认是否要删除数据。但即使在选择"否"后,它也会删除数据:

请参阅代码:-

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();" />

任何帮助。。!

只需将代码更新为

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();
        }
    }