我正在try 创建一个带有复选框的表,其中包含一个"全部选中"框. 但我有几个问题:

  1. 我找不到如何全部选中/取消选中它们(遍历和选中它们?)
  2. 如果我单击"全部选中"框,它将选中下一个复选框(正如当前代码的状态所预期的那样).但是,如果我取消选中列表中的第一个复选框,"全部选中"按钮就不会按预期工作.

我发现很难阅读《了解如何解决问题》中的文档.我只是想让它充当一个"正常"的"全部选中"复选框.

<script src="https://unpkg.com/hyperscript.org@0.9.12/dist/_hyperscript.min.js"></script>
<table id="larmlista" class="table table-zebra">
        <!-- head -->
        <thead>
        <tr>
            <th>
                <!-- TODO - this is where a lot of buggs lives -->
                <input id="select-all-larm" type="checkbox" class="checkbox checkbox-sm"
                       _="on click
                        if me.checked
                            get #larmlista then add @checked to the next <input/>
                        else
                            get #larmlista then remove @checked from the next <input/>
                            then remove me.checked
                        end
                "/>
            </th>
            <th>Plats</th>
            <th>Namn</th>
            <th>Ip</th>
        </tr>
        </thead>
        <tbody>
        <!-- row 1 -->
        <tr>
            <th>
                <input type="checkbox" class="checkbox checkbox-sm"/>
            </th>
            <td>Office</td>
            <td>Thing1</td>
            <td>10.102.0.1</td>
        </tr>
        <!-- row 2 -->
        <tr>
            <th>
                <input type="checkbox" class="checkbox checkbox-sm"/>
            </th>
            <td>Backyard</td>
            <td>Thing2</td>
            <td>10.102.0.23</td>
        </tr>
        <!-- row 3 -->
        <tr>
            <th>
                <input type="checkbox" class="checkbox checkbox-sm"/>
            </th>
            <td>Kitchen</td>
            <td>Thing3</td>
            <td>10.102.0.16</td>
        </tr>
        </tbody>
    </table>

"Final" code:

<script src="https://unpkg.com/hyperscript.org@0.9.12/dist/_hyperscript.min.js"></script>
<table id="larmlista" class="table table-zebra">
    <!-- head -->
    <thead>
    <tr>
        <th>
            <input id="select-all-larm" name="larm_all" type="checkbox" class="checkbox checkbox-sm"
                    _="on click set <input[name='larm']/>'s checked to my.checked"/>
        </th>
        <th>Id</th>
        <th>Område</th>
        <th>Namn</th>
        <th>Ip</th>
    </tr>
    </thead>
    <tbody>
    <!-- row 1 -->
    <tr>
        <th>
            <input id="1" type="checkbox" class="checkbox checkbox-sm" name="larm"
                    _="on click handleChecked(me)"/>
        </th>
        <td>1</td>
        <td>Office</td>
        <td>Thing1</td>
        <td>10.102.0.1</td>
    </tr>
    <!-- row 2 -->
    <tr>
        <th>
            <input id="2" type="checkbox" class="checkbox checkbox-sm" name="larm"
                    _="on click handleChecked(me)"/>
        </th>
        <td>2</td>
        <td>Backyard</td>
        <td>Thing2</td>
        <td>10.102.0.23</td>
    </tr>
    <!-- row 3 -->
    <tr>
        <th>
            <input id="3" type="checkbox" class="checkbox checkbox-sm" name="larm"
                    _="on click handleChecked(me)"/>
        </th>
        <td>3</td>
        <td>Kitchen</td>
        <td>Thing3</td>
        <td>10.102.0.16</td>
    </tr>
    </tbody>
</table>
<script type="text/hyperscript">
   def handleChecked(checkbox)
        if checkbox.checked
                if (<input[name='larm']/>'s checked) do not contains false
                set <input[name='larm_all']/>'s checked to checkbox.checked
                end
            else  
                set <input[name='larm_all']/>'s checked to my.checked
        end
    end
</script>

推荐答案

将复选框的名称设置为"foo"(最好为输入元素定义一个名称)

<input type="checkbox" class="checkbox checkbox-sm" name="foo" />

然后在您的"全选"复选框中使用HS代码:

_="on click set <input[name='foo']/>'s checked to my.checked"

如果您想坚持使用类名,则在每个复选框中添加类:

<input type="checkbox" class="checkbox checkbox-sm foo" />

然后使用以下命令:

_="on click set .foo.checked to my.checked"

Html相关问答推荐

如何让一个动态列表以网格方式水平显示

css网格区域的布局不展开,也不显示滚动条

有没有可能设置div的边框宽度相对于其父宽度和高度?'

如何在不影响子列表的情况下在HTML(OL Li)上同时加数字和列表?

为什么从输入步骤中的扣减不能按预期进行?

如何才能只给没有孟加拉语Kar符号的字母上色?

如何在伪元素背景中定位多个CSS径向梯度

如何在图像中垂直和水平居中显示文本?

如何保持嵌套进度条的背景色?

如何使用CSS创建文件夹选项卡的形状?

rvest: Select 不包含链接的段落(<;p>;)

带有排序元素的 CSS 网格

如何在悬停时使用 CSS 更改许多同级元素

如何围绕中心zoom svg 并使父级达到其子级大小的 100%

在HTML使用link标签导入外部CSS时出现元素link不允许使用尾斜杠错误

为什么
标签在 Edge 和 Firefox 中的编号会有两种不同的结果?

显示填充正方形的图形的简单页面的 HTML 代码

Bootstrap 5 英雄

在部分而不是正文中定义页面宽度

是否可以在换行时向锚标记添加填充?