jQuery Bootstrap 不使用复选框的“已检查”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16906018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap doesn't use "checked" attribute of checkbox
提问by Bandydan
I am using Bootstrap. I have a table with a checkbox in a header and in each collumn. I am trying to make "check all" functionality on jQuery, but it seems that bootstrap does not use checked attribute. As I see, it adds span tag around my checkbox and adds a class 'checked' to it. Is there any possibility to work with checked attr, or I must forget about it and write class checking and switching?
我正在使用引导程序。我有一个表格,在标题和每个列中都有一个复选框。我正在尝试在 jQuery 上创建“检查所有”功能,但引导程序似乎不使用已检查的属性。正如我所见,它在我的复选框周围添加了 span 标记,并向其添加了一个“已选中”的类。有没有可能使用checked attr,或者我必须忘记它并编写类检查和切换?
Below goes the part of code from the result HTML:
下面是结果 HTML 中的部分代码:
<td>
<div class="checker">
<span class="">
<input type="checkbox" class="payout_check" style="opacity: 0;">
</span>
</div>
</td>
This is unchecked one. The it is checked, third row changes to:
这是一个未经检查的。检查它,第三行更改为:
<span class="checked">
采纳答案by agraterol
this work for uncheck
这项工作取消选中
jQuery('span', $('#uniform-You_id_checkbox')).removeClass("checked");
$('#You_id_checkbox').removeAttr("checked");
回答by Fight code with code
You could use prop. I faced the same issue. On checking a checkbox, the attribute checked came as undefined. When i changed it to prop, it gave true and false value based on the state of the check box.
你可以使用prop。我遇到了同样的问题。在选中复选框时,选中的属性未定义。当我将其更改为 prop 时,它会根据复选框的状态给出 true 和 false 值。
$('#checkboxId').prop('checked');
回答by craighandley
If I am understanding your issue I think the answer to an older question should help.
如果我理解您的问题,我认为旧问题的答案应该会有所帮助。
$('#toggle-all').click(function() {
$('.btn-group input[type="checkbox"]').prop('checked', true);
});
回答by Jhonatascf
This worked for me:
这对我有用:
$('#form-new-event input[type=checkbox]').parents('span').removeClass("checked");
$('#form-new-event input[type=checkbox]').removeAttr("checked");
回答by Jason Stonebraker
Using jQuery
使用 jQuery
$("input[name='theCheckboxName']").is(":checked"); // Returns true or false
OR
或者
$("input[name='theCheckboxName']").prop("checked"); // Returns true or false
This inspects the "checked" property of the DOM element rather than the existence of the "checked" attribute on the tag. Using this method you avoid having to set and unset the "checked" attribute.
这会检查 DOM 元素的“已检查”属性,而不是标签上是否存在“已检查”属性。使用这种方法可以避免设置和取消设置“checked”属性。
In Markup
在标记中
<div class="checkbox">
<label>
<input name="theCheckboxName" type="checkbox" checked="checked"> <span>Yes</span>
</label>
</div>
This makes the checkbox checked by default. The "checked" property should still be used to determine state programmatically.
这使复选框默认选中。“已检查”属性仍应用于以编程方式确定状态。
Check out this link for more info:
查看此链接了解更多信息:
http://www.tutorialrepublic.com/faq/how-to-check-a-checkbox-is-checked-or-not-using-jquery.php
http://www.tutorialrepublic.com/faq/how-to-check-a-checkbox-is-checked-or-not-using-jquery.php
回答by Paulo Henrique Foxer
$(document).ready( function(){ $("[type=checkbox]").on("click", function(){ if ($(this).attr("checked")==undefined) { $(this).attr("checked","checked"); } else { $(this).attr("checked",false); } }); });
回答by Daniel Loureiro
$('#your-checkbox-id').parents('span').removeClass("checked").end().removeAttr("checked").change();
or
或者
$('#your-checkbox-id').prop('checked') && $('#your-checkbox-id').click();
PS: the last one will emulate a click (only if the checkbox is checked). If you don't want to trigger an event, use the first one and remove the "change()" call.
PS:最后一个将模拟点击(仅当复选框被选中时)。如果您不想触发事件,请使用第一个并删除“change()”调用。