微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不更改var参数的情况下选择所有复选框

如何解决如何在不更改var参数的情况下选择所有复选框

我使用 viewmodel 并且我有带复选框的表格,我只能一个一个地检查,但我需要检查所有选项但我需要定义我的 var 值,因为我将这些值传递给控制器​​并执行某些操作。

      $('.checktip').click(function () {
      var idemployee = $('.idemployee').val();
      var idtip= $(this).attr('idtip');
      var chk = $(this).prop('checked');
      $.ajax({
            url: UrlSettingsDocument.Books,data: { idemployee: idemployee,idtip: idtip,chk: chk },type: "POST",success: function (result) {
      if (result.result == "Redirect") {
      window.location = result.url;
     }
     }
     });
    });

我的 .cshtml

<table class="table table-striped grid-table">
        <tr>
        <th>Samp</th>
        <th>Id of Book</th>
        <th>
         //Button for check all *(NOT IMPLEMENTED)*
         <button type="button" class="checkall">Select/deselect</button>
        </th>
        </tr>
        @foreach (var item in (IEnumerable<cit.Models.getCheIdTip_Result>)Model)
        {
        <tr>
        <td>@item.idtip</td>
        <td>@item.tipname</td>
        <td>
        <div class="pure-checkBox">
        <input type="checkBox" idtip="@item.idtip" class="checktip" checked="@(item.idemployee == ViewBag.idemployee ? true : false)" name="@item.id.ToString()" id="@item.id.ToString()" />
        <label for="@item.id.ToString()"></label>
        </div>
        </td>
        </tr>
        }
    </table>
    <input type="hidden" value="@ViewData["idemployee"]" name="idemployee" id="idemployee" class="idemployee" />
</div>

解决方法

使用复选框来选中所有复选框而不是按钮。

在检查更改时,使用类检查/取消选中表中的所有复选框。

$('#checkall').on('change',function() {
  $('.checktip:checkbox').prop('checked',$(this).is(":checked"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped grid-table">
  <tr>
    <td><input type="checkbox" id="checkall" />Check All</td>
    <td><input type="checkbox" class="checktip" /></td>
    </td>
    </td>
    </td>
    <td><input type="checkbox" class="checktip" /></td>
    </td>
    </td>
    <td><input type="checkbox" class="checktip" /></td>
    </td>
    <td><input type="checkbox" class="checktip" /></td>
  </tr>
</table>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。