【问题标题】:Multi delete checkbox on button click does not work as required按钮单击时的多重删除复选框无法按要求工作
【发布时间】:2014-12-26 14:57:48
【问题描述】:

我有一个 grdiview,我在其中添加了多重删除功能。请参阅代码供您参考:-

<script type="text/javascript">
    function ValidateAll() {
        var chkselectcount = 0;
        var gridview = document.getElementById('<%= grdTeacherProfile.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;
        }
    }
</script>

另见按钮html:-

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

问题是, 当我检查复选框并单击删除按钮时,它会要求确认。当我点击取消时,它仍然会从Gridviewsql table 中删除该行。

我应该怎么做才能正常工作。 ?请推荐

【问题讨论】:

  • OnClick="btnDelete_Click" 尝试删除它。
  • @Jai:很好,Shekar 给出的答案是有效的..

标签: javascript jquery asp.net checkbox


【解决方案1】:

我认为你需要使用

return ConfirmationBox();

而不是

ConfirmationBox();

所以你的代码变成了

function ValidateAll() {
    var chkselectcount = 0;
    var gridview = document.getElementById('<%= grdTeacherProfile.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 {
        return ConfirmationBox();
    }
}

您需要从OnClientClick 中删除javascript:,您可以使用OnClientClick="return ValidateAll();"

【讨论】:

  • 它不起作用,它仍在删除该行。你检查我的按钮 html 代码了吗?
  • 按钮客户端点击似乎没问题。虽然你可以删除 javascript: 并写 juse OnClientClick="return ValidateAll();"
  • 是的,现在正在工作.. 请更新您的答案,以便我可以标记它.. :) 谢谢 Shekhar。
猜你喜欢
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 2012-05-13
相关资源
最近更新 更多