javascript - 取消选中一个复选框时如何取消选中以下所有复选框

标签 javascript c# asp.net checkbox checkboxfor

我有一组已标记的动态复选框。当我单击其中一个时,应取消选中下面的所有复选框,如果再次标记,则再次标记上面的所有复选框。请问有人可以帮我吗?

谢谢。

@for (int i = 0; i < Model.transportDate.Count(); i++)
{
                    <tr>
                        <td>
                            @Html.CheckBoxFor(Model => Model.transportDate[i].selection)
                        </td>
                        <td>
                            @Html.TextBoxFor(Model => Model.transportDate[i].Date)
                        </td>
                    </tr>
}

Overview of question

最佳答案

您可以使用 javascript 在客户端执行此操作。参见示例。

$('#tbl [type=checkbox]').click(function() {
  var $this = this; //save clicked chk
  if ($(this).is(':checked')) //nothing to do
    return;
  var uncheck = false;
  $(this).parents('table') //get container
    .find('[type=checkbox]') //get siblings
    .each(function() { //iterate
      if (this === $this) //found
        uncheck = true;
      if (uncheck)
        this.checked = false; //clear checkbox
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbl">
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
  <tr>
    <td><input type="checkbox" checked="checked" /></td>
    <td>other</td>
  </tr>
</table>

关于javascript - 取消选中一个复选框时如何取消选中以下所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571809/

相关文章:

c# - C# 中的 JSON 到 CSV 和 CSV 到 JSON

c# - 在 docker 中连接到 MySQL 实例时出错

asp.net - 在 ASP.NET MVC 中为部分 View 创建 Controller

asp.net - 在 Web 应用程序之间共享 asp.net 资源文件

javascript - 何时调用服务器 onClick 与 OnClientClick

javascript - 您如何使用 javascript 在 FF 中切换 div 的可见性? (IE 和 Chrome 工作正常)

javascript - jQuery 插件 - 公共(public)方法 - 仅针对一个元素的数据

javascript - 解析云代码 Mandrill Promise

javascript - 在表单验证上显示模态

c# - 将值绑定(bind)到复杂类型