取消选中一组检查按钮

Uncheck a group of check buttons

本文关键字:一组 检查 按钮 取消      更新时间:2023-09-26

我正在尝试取消选中onclick事件中的一组检查按钮:

.HTML:

 </div>
            <button onclick="getChart(); uncheck();" type="submit" class="btn btn-default" id="refreshChart">Request</button>
        </div>
            <form class="form">
                <div class="btn-group btn-group-justified " data-toggle="buttons" name="chartSelect">
                    <label class="btn btn-info" id ="hide">
                        <input name="hide" type="checkbox" ><span>Hide</span>
                    </label>
                    <label class="btn btn-info">
                        <input name="total" id="total" type="checkbox">Total
                    </label>
                    <label class="btn btn-info">
                        <input name="max" id="max" type="checkbox">Max
                    </label>
                    <label class="btn btn-info">
                        <input name="mean" id="mean" type="checkbox">Mean
                    </label>
                    <label class="btn btn-info">
                        <input name="min" id="min" type="checkbox">Min
                    </label>
                    <label class="btn btn-info">
                        <input name="extrapo" id="extrapolation" type="checkbox">Extrapo
                    </label>
                    <label class="btn btn-info">
                        <input name="all" id="all" type="checkbox">All
                    </label>
                </div>
            </form>

我什至只是想让一个按钮取消选中,但没有成功。

.JS:

function uncheck() {
    if ($('[name=max]').is(':checked')) {
        $('[name=max]').attr('checked', false);
    }
}

调用过滤器似乎is这样的:

$("div[data-toggle='buttons'] input[type='checkbox']").is(":checked")

不起作用。但是如果你把它和选择器放在一起,它就可以工作了。所以这很好:

$("div[data-toggle='buttons'] input[type='checkbox']:checked")

现在您可以取消选中所有选项:

$("div[data-toggle='buttons'] input[type='checkbox']:checked").removeAttr("checked");

试试这个小提琴

看起来您的代码正在运行,只是您调用了未定义的函数getChart()。当我删除该函数调用时,单击按钮时未选中复选框。

https://jsbin.com/yakiyopoye/edit?html,output