全选/部分选按钮

样式:

<style>
        * {
            margin: 0;
            padding: 0;
        }

        table {
            margin: 100px auto;
            width: 400px;
            height: 180px;
            text-align: center;
            border-collapse: collapse;
        }

        thead {
            background-color: #0089CC;
            height: 30px;
        }

        tbody {
            background-color: #F5EFF5;
        }

        tbody td {
            border: 1px solid #E5E0E6;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <tr>
                <th>
                    <input type="checkbox" name="" id="all">
                </th>
                <th>
                    商品
                </th>
                <th>价钱</th>
            </tr>
        </thead>

        <tbody id="tb">
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>华为</td>
                <td>10000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>小米</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>phone</td>
                <td>1200</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>windows phone</td>
                <td>9500</td>
            </tr>
        </tbody>
    </table>

下面是重点代码:

<script>
        var all = document.querySelector("#all");
        var tb = document.querySelector("#tb").getElementsByTagName("input");
        // var flag = 0;
        all.onclick = function () {//绑定点击事件
            //this.checked可以得到当前复选框的状态,如果是true,则表示选中
            console.log(this.checked);
            // 全选/全不选
            for (let i = 0; i < tb.length; i++) {
                    tb[i].checked = this.checked;
            }    
        }

        // ------------------------------------------------
        // 如果有1个不选,则all按钮不会被选,如果小按钮全选,则all会被选上
        for (let i = 0; i < tb.length; i++) {//给4个小按钮添加点击事件
            tb[i].onclick=function(){
                console.log("小按钮被选中");
                //flag判断全选按钮是否被选中 
                var flag=true;
                // 每次点击复选框都要循环检查4个小按钮,是否被全选
                for (let i = 0; i < tb.length; i++) {
                    if (!tb[i].checked) {//如果这个按钮没有被选中
                        // all.checked=false;// 如果有一个小按钮,没有被选中,则大按钮不会被选中。
                        flag=false;
                    }
                }
                // 如果所有的小按钮被选中,则令大按钮为真
                all.checked=flag;
            }
        }
    </script>

在dege上打断点可以得到:
先执行绑定每个按钮的点击事件,后续如果点击某个按钮再执行该按钮指定的其他操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果你想要在Python的Qt的QWidget中实现复全选的功能,你可以按照以下步骤进行操作: 1. 导入必要的Qt类和函数: ```python from PyQt5.QtWidgets import QWidget, QCheckBox from PyQt5.QtCore import Qt ``` 2. 创建一个QWidget实例: ```python class MyWidget(QWidget): def __init__(self): super().__init__() ``` 3. 在init方法中创建复框(checkbox)并将其加入QWidget中: ```python self.checkbox1 = QCheckBox('项1') self.checkbox2 = QCheckBox('项2') self.checkbox3 = QCheckBox('项3') self.checkbox1.stateChanged.connect(self.on_select_all) self.checkbox2.stateChanged.connect(self.on_select_all) self.checkbox3.stateChanged.connect(self.on_select_all) layout = QVBoxLayout() layout.addWidget(self.checkbox1) layout.addWidget(self.checkbox2) layout.addWidget(self.checkbox3) self.setLayout(layout) ``` 4. 创建一个on_select_all方法用于实现全选功能: ```python def on_select_all(self, state): if state == Qt.Checked: self.checkbox1.setChecked(True) self.checkbox2.setChecked(True) self.checkbox3.setChecked(True) else: self.checkbox1.setChecked(False) self.checkbox2.setChecked(False) self.checkbox3.setChecked(False) ``` 这样,当你择或取消全选框时,所有复框的状态都会同步变化。 希望以上的答案能对你有所帮助! ### 回答2: 在Python的QtWidgets中,我们可以通过编写代码来实现复框的全选功能。 首先,我们需要导入QtWidgets模块,并创建一个QtWidgets.QApplication实例,以便能够使用QtWidgets功能。 接下来,我们需要创建一个QtWidgets.QWidget窗口,并设置窗口的大小和标题。 然后,我们可以在窗口中创建一个QtWidgets.QCheckBox复框,并设置其文本。 接着,我们可以创建一个QtWidgets.QHBoxLayout布局管理器,并将复框添加到布局中。 接下来,我们可以通过创建一个QtWidgets.QPushButton按钮,并设置其文本为“全选”,将按钮添加到布局中。 然后,我们可以定义一个名为“select_all”的函数,在函数中使用循环遍历布局中的所有窗口小部件,并设置它们的复框状态为已中。 最后,我们将按钮的clicked信号连接到“select_all”函数,并将布局设置为窗口的主布局。 最后,我们通过调用QtWidgets.QApplication的exec_()方法来运行应用程序的主循环。 以下是代码示例: ``` import sys from PyQt5 import QtWidgets def select_all(): for widget in layout.children(): if isinstance(widget, QtWidgets.QCheckBox): widget.setChecked(True) app = QtWidgets.QApplication(sys.argv) window = QtWidgets.QWidget() window.setWindowTitle("全选框") window.resize(300, 200) checkbox = QtWidgets.QCheckBox("复框") layout = QtWidgets.QHBoxLayout() layout.addWidget(checkbox) button = QtWidgets.QPushButton("全选") button.clicked.connect(select_all) layout.addWidget(button) window.setLayout(layout) window.show() sys.exit(app.exec_()) ``` 通过运行上述代码,我们可以创建一个带有复框和全选按钮的窗口。当我们点击按钮时,所有复框都将被中。 ### 回答3: 要实现Python PyQt中复框的全选功能,可以通过以下步骤来完成: 首先,我们需要导入相应的模块,包括`PyQt5.QtWidgets`用于创建GUI,以及其他需要的模块。 接着,在主程序中创建一个QWidget窗口,并设置布局为QVBoxLayout。 然后,创建一个QGroupBox小部件,并将其添加到主窗口布局中。 在QGroupBox小部件中,创建一个QVBoxLayout,并将其设置为布局。 然后,使用一个循环来创建多个QCheckBox复框,并将它们添加到QGroupBox的布局中。 在复框的停靠部分,添加一个QCheckBox用于全选,并为其添加信号槽方法。 在槽方法中,获取QGroupBox的布局,并使用循环遍历每个子部件,将其状态设置为与全选框相同。 最后,运行主程序并查看结果。 下面是一个示例代码: ``` from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QGroupBox, QCheckBox, QHBoxLayout def selectAll(): for i in range(group_box_layout.count()): checkbox = group_box_layout.itemAt(i).widget() if checkbox != select_all_checkBox: checkbox.setChecked(select_all_checkBox.isChecked()) if __name__ == '__main__': app = QApplication([]) window = QWidget() main_layout = QVBoxLayout() window.setLayout(main_layout) group_box = QGroupBox("择") group_box_layout = QVBoxLayout() group_box.setLayout(group_box_layout) options = ["项1", "项2", "项3", "项4"] select_all_checkBox = QCheckBox("全选") select_all_checkBox.stateChanged.connect(selectAll) group_box_layout.addWidget(select_all_checkBox) for option in options: checkbox = QCheckBox(option) group_box_layout.addWidget(checkbox) main_layout.addWidget(group_box) window.show() app.exec_() ``` 运行以上代码,你将看到一个带有多个复框的窗口,其中包括一个全选框。择或取消全选框时,其他复框的状态将与之同步。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值