【问题标题】:alert if no check box is selected and button disabled if all checkbox are disabled in a grid on page load by Jquery如果在 Jquery 加载页面时在网格中禁用了所有复选框,则如果没有选中复选框并禁用按钮,则发出警报
【发布时间】:2017-10-21 15:03:45
【问题描述】:

如果在 jQuery 加载页面时网格中的所有复选框都被禁用,如果没有选中复选框并禁用按钮,如何发出警报

试过

 $( document ).ready(function() {
     $('input[type=checkbox]').change(function () {
         disableCheckbox();
    });

    disableCheckbox = function () {
        var count = $('input[type=checkbox]:checked').length;
        $('btnCancelItem').prop('disabled', count == 0);
    };

    disableCheckbox();
});

<asp:LinkButton CssClass="btn btn-primary" ID="btnCancelItem" runat="server" CausesValidation="False"OnClientClick="return Confirmationbox();">&nbsp;Cancel Item</asp:LinkButton>
<asp:HiddenField id="hdnval" value=0 runat="server"/>

【问题讨论】:

  • 这里有什么问题?你在哪里面临问题?什么不工作?你能解释一下吗?
  • 如果没有选中复选框,如何发出警报?
  • 如果 count == 0,则提醒
  • 未选中复选框时发出警报?
  • 是的,根据代码计数是检查复选框的数量,如果计数为 0,则不检查任何复选框。

标签: javascript jquery asp.net


【解决方案1】:

您缺少 # 以使按钮被禁用

$( document ).ready(function() {
  $('input[type=checkbox]').change(function () {
    disableCheckbox();
  });

  disableCheckbox = function () {
   var count = $('input[type=checkbox]:checked').length;
   if (count == 0) { alert("nothing selected") } 
   $('#btnCancelItem').prop('disabled', count == 0);
  };

  disableCheckbox();
});

【讨论】:

  • 如果所有复选框都被禁用,按钮将被禁用
【解决方案2】:
  disableCheckbox = function () {
                    //checked check-boxes length
                    checkedCount = $('#CP_Main_gvPOItems input[type=checkbox]:checked').length;

                    //check-boxes length
                    checkboxCount = $('#CP_Main_gvPOItems input[type=checkbox]').length;

                    //if no check-box is selected then alert
                    //                if (checkedCount == 0) {
                    //                    alert('No check-box is selected');
                    // }
                    //check for all disabled check-boxes
                    //var disableCheckBox = 0;
                    $('#CP_Main_gvPOItems input[type=checkbox]').each(function () {
                        if ($(this).is(':disabled')) {
                            disableCheckBox++;
                        }
                    });

                    //if all check-boxes are disabled then disable button
                    if (checkboxCount == disableCheckBox) {

                        $('#CP_Main_btnCancelItem').attr("disabled", "disabled"); //# is missing
                    }
                };




                disableCheckbox();

                $("#CP_Main_btnCancelItem").button().click(function () {

                    var checkedCount = $('#CP_Main_gvPOItems input[type=checkbox]:checked').length;

                    var isDisabled = $(checkedCount).is(':disabled');
                    // alert(isDisabled);
                    var status = false;
                    if (checkedCount == 0 && isDisabled) {
                        alert('No check-box is selected');
                        status = false;
                    }
                    else if (!isDisabled && checkedCount > 0) {
                        alert('Are you sure you want to cancel selected Record(s)?');
                        status = true;
                    }
                    return status;
                });

【讨论】:

  • @Alive to Die 检查
猜你喜欢
  • 2016-03-12
  • 2015-09-22
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
相关资源
最近更新 更多