7

我正在使用引导程序。我在标题和每个列中有一个带有复选框的表格。我正在尝试在 jQuery 上创建“检查所有”功能,但似乎引导程序不使用检查属性。正如我所看到的,它在我的复选框周围添加了 span 标签,并为其添加了一个“已选中”类。是否有可能使用已检查的 attr,或者我必须忘记它并编写类检查和切换?

下面是结果 HTML 中的部分代码:

<td>
  <div class="checker">
    <span class="">
      <input type="checkbox" class="payout_check" style="opacity: 0;">
    </span>
  </div>
</td>

这是未经检查的。检查后,第三行更改为:

<span class="checked">
4

7 回答 7

11

你可以使用prop。我遇到了同样的问题。在选中复选框时,选中的属性未定义。当我将其更改为道具时,它会根据复选框的状态给出真假值。

$('#checkboxId').prop('checked');
于 2015-02-19T10:51:59.313 回答
5

这项工作取消选中

jQuery('span', $('#uniform-You_id_checkbox')).removeClass("checked");

$('#You_id_checkbox').removeAttr("checked");
于 2013-07-10T06:36:28.557 回答
5

如果我理解您的问题,我认为对旧问题的回答应该会有所帮助。

$('#toggle-all').click(function() {
    $('.btn-group input[type="checkbox"]').prop('checked', true);
});

http://jsfiddle.net/mmfansler/g3mu8/

于 2013-06-03T21:23:38.763 回答
3

这对我有用:

$('#form-new-event input[type=checkbox]').parents('span').removeClass("checked");

或者

$('#form-new-event input[type=checkbox]').removeAttr("checked");
于 2014-08-07T14:59:10.360 回答
1

使用 jQuery

$("input[name='theCheckboxName']").is(":checked"); // Returns true or false

或者

$("input[name='theCheckboxName']").prop("checked"); // Returns true or false

这会检查 DOM 元素的“checked”属性,而不是标签上是否存在“checked”属性。使用这种方法可以避免设置和取消设置“checked”属性。


在标记中

<div class="checkbox">
    <label>
        <input name="theCheckboxName" type="checkbox" checked="checked"> <span>Yes</span>
    </label>
</div>

这使复选框默认选中。“checked”属性仍应用于以编程方式确定状态。


查看此链接了解更多信息:

http://www.tutorialrepublic.com/faq/how-to-check-a-checkbox-is-checked-or-not-using-jquery.php

于 2014-08-07T19:04:32.943 回答
1
    $(文档).ready(函数(){
        $("[type=checkbox]").on("点击", function(){
            if ($(this).attr("checked")==undefined) {
                    $(this).attr("checked","checked");
                } 别的 {
                   $(this).attr("checked",false);
                }
        });
    });

于 2013-11-27T15:35:46.777 回答
0
$('#your-checkbox-id').parents('span').removeClass("checked").end().removeAttr("checked").change();

或者

$('#your-checkbox-id').prop('checked') && $('#your-checkbox-id').click();

PS:最后一个将模拟单击(仅在选中复选框时)。如果您不想触发事件,请使用第一个并删除“change()”调用。

于 2014-08-25T18:26:39.530 回答