[jQuery]檢查gridview裡面checkbox至少選擇一項

  • 1041
  • 0
  • 2019-08-13

摘要:[jQuery]檢查gridview裡面checkbox至少選擇一項

 $(document).ready(function () {


            ////批次刪除按鈕事件
            var tdbBatchDelete = $("#<%=tdbBatchDelete.ClientID%>");
            tdbBatchDelete.click(function () {
                var chkCount = $("#<%=gvMain.ClientID%> input[id*=chk]:checkbox:checked").size();
                if(chkCount ==0){
                    alert('請至少選擇一項!');
                    return false;
                }
                return confirm('您確定要刪除?');
            });

        });


補充:checkbox list如何檢查至少勾選一個
javascript:

//count變數就是checkbox list打勾的數量
var count = $("input[type=checkbox][name=SelectedRoles]:checked").length;


html的checkbox list:

<div class="col-md-10">
    <div class="row">
        <label class="col-form-label col-md-4 text-right" for="ReasonIdxList">事由</label>
        <div class="col-md-8">
            <div class="form-group" id="chkList">
                <div class="form-check-inline">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" name="SelectedRoles" value="1" class="custom-control-input" id="出納事由1">
                        <label class="custom-control-label" for="出納事由1">出納事由1</label>
                    </div>
                </div>
                <div class="form-check-inline">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" name="SelectedRoles" value="2" class="custom-control-input" id="出納事由2">
                        <label class="custom-control-label" for="出納事由2">出納事由2</label>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>