【问题标题】:how to check all and toggle at the same time [duplicate]如何检查所有并同时切换[重复]
【发布时间】:2016-09-29 19:26:57
【问题描述】:

我有一些复选框可以切换(显示和隐藏)内容。他们工作正常。但是当我添加一个检查所有复选框时,它会检查所有复选框,但内容不再切换。我对为什么以及如何让它发挥作用感到困惑。

我的html:

<p><input type="checkbox" class="section-select one">one </p>
<p><input type="checkbox" class="section-select two">two</p>
<p><input type="checkbox" class="section-select three">three</p>
<p><input type="checkbox" class="check-all">check all</p>
<div id="one">One</div>
<div id="two">two</div>
<div id="three">three</div>

我的js:

$("input.check-all").change(function(){
    $("input.section-select").prop('checked', $(this).prop("checked"));
})

$("input.section-select").change(function(){
    var inputClass = $(this).attr("class").split(' ').pop();
    var sectionToHide = "#" + inputClass;
    $(sectionToHide).toggle();
});

这是一个小提琴:https://jsfiddle.net/jawxvc49/

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您可以使用trigger() 来触发更改事件。

    $("input.check-all").change(function(){
        $("input.section-select").prop('checked', $(this).prop("checked")).trigger('change');
    })
    

    【讨论】:

    • 完美运行,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2017-06-12
    • 2023-04-02
    相关资源
    最近更新 更多