使用MVC检查至少一个自动生成jquery datatable复选框

问题描述 投票:0回答:1

在提交表单之前,尝试确保用户至少选中一个复选框。目前我的JavaScript无效。该复选框在jquery数据表中自动生成。即使没有选中表中的复选框,代码仍然会进入控制器

HTML

 <table id="scheduleAppointment-data-table" class="table table-striped table- bordered" style="width:100%;"></table>    
  <input type="submit" value="Send" class="btn btn-default" id=btnSubmit onclick="Validate()"/>

数据表

      // Auto generate checkbox
      "columns": [
             {
               targets": [0],
                "data": "EDMXID", "autoWidth": true,              
                 "render": function (data, type, full) {
                 return '<input type="checkbox" id="EDMXID" name="EDMXID" value="'+full.EDMXID+'"/>';
              },
               }]

使用Javascript

 function Validate() {
        var allOk = true;
     $(scheduleAppointment-data-table).find("tbody tr").each(function (){
            var row = $(this);
            var checked = row.find($(':checkbox:checked')).length > 0
            if (!checked) {
                allOk = false;
                alert('At least One Appointment Should Be Selected');
                return allOk;
            }
        });
        return allOk;
    }
javascript model-view-controller datatable
1个回答
0
投票

在Davidkonrad评论的帮助下,我终于使用了下面的JavaScript代码

 $(function () {
        $('input[id$=btnSubmit]').click(function (e) {
            $('#scheduleAppointment-data-table').find("tbody tr").each(function () {
                var row = $(this);
                var checked = row.find($(':checkbox:checked')).length > 0
                if (!checked) {
                    alert('At least One Appointment Should Be Selected');
                    e.preventDefault();
                }
            });
        });
    });
© www.soinside.com 2019 - 2024. All rights reserved.