将复选框变成复选框

时间:2018-12-19 20:51:08

标签: javascript jquery html css checkbox

我正在尝试选中以下复选框:

enter image description here

进入这些复选框(我将其称为 CheckButtons ):

enter image description here

正下方是当前复选框的代码:

        @foreach (var department in Model.Select(u => new { u.DepartmentId, u.DepartmentName }).Distinct().ToDictionary(u => u.DepartmentId, u => u.DepartmentName).OrderBy(u => u.Value))
    {
        i++;
        <text> &nbsp; &nbsp;</text>
@department.Value <input name="department_chkbox" type="checkbox" value="@department.Key" />
        if (i > 5)
        {
            <text><br></text>
            i = 0;
        }
    }

所需的HTML如下,但告诉我的并不多:

<td id="checkboxcontainer">
     <input type="checkbox" name="statusId" value="1" id="ckActive" checked="checked" /><label for="ckActive">Active</label>
     <input type="checkbox" name="statusId" value="2" id="ckLeave" /><label for="ckLeave">Leave</label>
     <input type="checkbox" name="statusId" value="3" id="ckSusp" /><label for="ckSusp">Suspended</label>
     <input type="checkbox" name="statusId" value="4" id="ckTerm" /><label for="ckTerm">Terminated</label>
</td>

有人知道我将复选框代码编写为使复选框变成“ checkbuttons”的说法,但是我无权访问 check button 码。我假设这是在eitehr Javascript或Jquery中完成的。

也没有课程

1 个答案:

答案 0 :(得分:4)

脱离所需的HTML,这是仅使用CSS的相对简单的解决方案。当然,您需要对其进行调整,以使其看起来完全符合您的期望。

#checkboxcontainer {
  display: flex;
}

input[type=checkbox] {
  display: none;
}

input[type=checkbox] + label {
  display: block;
  min-width: 100px;
  border: solid #999;
  border-width: 1px 1px 1px 0;
  background: #eee;
  margin: 0;
  padding: 2px 0;
  text-align: center;
}

input[type=checkbox]:checked + label {
  background: #ccc;
}

input[type=checkbox] + label:first-of-type {
  border-radius: 3px 0 0 3px;
  border-left-width: 1px;
}
input[type=checkbox] + label:last-of-type {
  border-radius: 0 3px 3px 0;
}
<div id="checkboxcontainer">
  <input type="checkbox" name="statusId" value="1" id="ckActive" checked="checked" /><label for="ckActive">Active</label>
  <input type="checkbox" name="statusId" value="2" id="ckLeave" /><label for="ckLeave">Leave</label>
  <input type="checkbox" name="statusId" value="3" id="ckSusp" /><label for="ckSusp">Suspended</label>
  <input type="checkbox" name="statusId" value="4" id="ckTerm" /><label for="ckTerm">Terminated</label>
</div>

要使用Razor生成此HTML,您将具有以下内容:

<div id="checkboxcontainer">
  @foreach (var department in Model.Select(u => new { u.DepartmentId, u.DepartmentName }).Distinct().ToDictionary(u => u.DepartmentId, u => u.DepartmentName).OrderBy(u => u.Value))
  {
    i++;
    <input name="department_chkbox" type="checkbox" value="@department.Key" id="department_chkbox@(i)" /><label for="department_chkbox@(i)">@department.Value</label>
  }
</div>