简体   繁体   English

如何使用JavaScript中的check-all框创建与内容相关的复选框?

[英]How can I make content-dependent checkboxes, with a check-all box in JavaScript?

I have searched for hours, and tried many things, but I can't get it to work. 我已经搜索了几个小时,并尝试了很多东西,但我无法让它工作。

This is my problem: I'm trying to make some sort of summary of a book series I enjoy, in html files and I want to be able to show/hide content based on a few checkboxes at the top of a page. 这是我的问题:我试图在html文件中对我喜欢的书系列进行某种总结,我希望能够根据页面顶部的几个复选框显示/隐藏内容。

So when I check only the "book 2" checkbox, no content from other books may appear. 因此,当我仅选中“书籍2”复选框时,不会显示其他书籍中的内容。

Secondly I want a check-all button, too. 其次,我也想要一个全包按钮。 For these 2 things I have 2 functions, and they do work independently, but when I click the check-all box, the content does not automatically appear, and I'm stuck to why that is. 对于这两件事我有2个功能,并且它们可以独立工作,但是当我点击check-all框时,内容不会自动出现,而且我坚持为什么会这样。

Here's my code (HTML): 这是我的代码(HTML):

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script src="Scripts.js"></script>
    </head>
<body>
    <p> <input type="checkbox" onchange="ToggleCheck(this);"> Check all</p>
    <p> <input type="checkbox" name="bookbox" onchange="ShowHide('Book0', this);"> Book 0</p>
    <p> <input type="checkbox" name="bookbox" onchange="ShowHide('Book1', this);"> Book 1</p>
    <p> <input type="checkbox" name="bookbox" onchange="ShowHide('Book2', this);"> Book 2</p>
    <p> <span class="Book0">Book 0 content. </span>
        <span class="Book1">Book 1 content. </span>
        <span class="Book2">Book 2 content. </span>
        <span class="Book1">Book 1 content. </span>
    </p>
</body>
</html>

And here's the attached JS file 这是附加的JS文件

function ToggleCheck(source) {
    var checkboxes = document.getElementsByName('bookbox');
    for(var i=0, n=checkboxes.length;i<n;i++) {
        checkboxes[i].checked = source.checked;
    }
}

function ShowHide(a, b) {
    var vis = (b.checked) ? "inline" : "none";
    var book = document.getElementsByClassName(a);
    for (var i = 0; i < book.length; i++) {
        book[i].style.display = vis;
    }   
}

I do apologize to sign up for this site just for this, but if someone could help me, I would be very gratefull. 我很抱歉为此注册此网站,但如果有人能帮助我,我会非常感激。 Thank you in advance! 先感谢您!

When you do the ToggleCheck(...) function it appears that the onchange event is not triggered. 当您执行ToggleCheck(...)函数时,似乎未触发onchange事件。 You can either trigger it manually inside this method by following this: How can I trigger an onchange event manually? 您可以通过以下方式在此方法中手动触发它: 如何手动触发onchange事件?

or you can change the way you handle things a little bit and then call ShowHide(...) inside the for-loop in ToggleCheck(...) . 或者你可以改变你处理事物的方式,然后在ShowHide(...)的for循环中ToggleCheck(...)

This edit makes it work (using the first and easiest solution): 此编辑使其工作(使用第一个也是最简单的解决方案):

function ToggleCheck(source) {
    var checkboxes = document.getElementsByName('bookbox');
    for (var i = 0, n = checkboxes.length; i < n; i++) {
        checkboxes[i].checked = source.checked;
        checkboxes[i].onchange(); //manual trigger of the onchange event
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将 v-model 属性添加到复选框时,全选停止工作 - Check-all stops working when I add v-model attribute to the checkboxes 选中“所有复选框”后,请选中其他复选框 - When “check-all” box checked, check others 可根据内容扩展尺寸 - Collapsible with content-dependent expanded size 如何在 React 的复选框列表中一次只选中一个复选框而不是所有复选框 - How can I check only one check box at a time instead of all in a list of checkboxes in React Telerik RadComboBox:每次都选中“全部选中”时,我得到JavaScript错误:此页面上的脚本导致您的Web浏览器运行缓慢 - Telerik RadComboBox: Everytime Check-All is selected I get the JavaScript error: A script on this page is causing your web browser to run slowly 如何检查全部 <p:selectBooleanCheckbox/> 通过使用javaScript选中一个复选框来实现布尔复选框 - How to check all <p:selectBooleanCheckbox/> boolean Checkboxes by checking one check box with javaScript 如何检查特定列 javascript 中的所有复选框? - How to check all checkboxes in a specific column javascript? 使用 JavaScript 检查所有复选框 - Check all checkboxes with JavaScript 如何使我的全选复选框忽略禁用的项目? - How can I make my select all check box ignore disabled items? 如何使因变量在HTML / javascript中响应和更新? - How can I make dependent variables respond and update in HTML/javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM