【问题标题】:How to select all checkboxes without changing var parameters如何在不更改 var 参数的情况下选择所有复选框
【发布时间】:2021-01-08 09:44:08
【问题描述】:

我使用 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>

【问题讨论】:

  • 您好,您能否详细说明您的问题。
  • @Swati 我的 js 代码一个一个地检查复选框,当我重新加载它时它被保存了,没关系,但现在我想添加 &lt;button type="button" class="checkall"&gt;Select/Deselect&lt;/button&gt; 来检查它们,当我重新加载它需要都得救了。我使用这个 var 元素 var idemployee var idtipvar var chk 并将它们传递给控制器​​,并为每个员工执行保存在数据库复选框中。那么如何通过传递相同的 var 元素来做到这一点,但要检查点击功能上的所有框。
  • 所以你需要通过所有的复选框idtip & checked status ?
  • @Swati 是的,这就是我需要的。
  • 您可以通过loopchecked 值然后将这些值推送到json array 尝试让我知道您是否仍然无法正常工作。 This 答案应该会有所帮助。

标签: javascript c# jquery ajax asp.net-mvc


【解决方案1】:

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

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

$('#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>

【讨论】:

  • 我无法将此代码传递给控制器​​,因此我可以处理代码,在我的解决方案中,我定义了 var 并使用 ajax 数据在控制器 public JsonResult CheckUsers(int idemployee, int idtip, bool chk) 中使用它,但这样我只能检查逐个。在您的解决方案中,我没有定义变量。
猜你喜欢
  • 2021-11-27
  • 2011-04-25
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 2014-01-29
相关资源
最近更新 更多