以编程方式取消选中 jQuery UI 复选框按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7389303/
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
Programmatically uncheck jQuery UI checkbox button
提问by A. Murray
I have an HTML checkbox element on my page like so:
我的页面上有一个 HTML 复选框元素,如下所示:
<input type="checkbox" id="locked" /><label for="locked">Locked</label>
And I make a call inside my $(document).ready() to change this to be a jQuery UI checkbox button like so:
我在我的 $(document).ready() 中调用以将其更改为 jQuery UI 复选框按钮,如下所示:
$('#locked').button({
'icons': {
'primary': 'ui-icon-unlocked'
},
'label': 'Unlocked'
});
Background context being that the user can use this button to lock/unlock a particular entity so that a background process will not alter it and it starts with an 'Unlocked' status. If javascript is not turned on, the user sees a checkbox and the label 'Locked' next to it.
后台上下文是用户可以使用此按钮锁定/解锁特定实体,以便后台进程不会更改它并且它以“未锁定”状态开始。如果 javascript 没有打开,用户会看到一个复选框和旁边的标签“已锁定”。
I want to be able to programmatically check/uncheck this checkbox button. I've tried:
我希望能够以编程方式选中/取消选中此复选框按钮。我试过了:
$('#locked').attr('checked', false);
But the checkbox button does not update to reflect the underlying control's checked status.
但是复选框按钮不会更新以反映底层控件的选中状态。
I could test the checkbox's checked property then do a .click() if it doesn't match what I want but that doesn't sound very elegant.
我可以测试复选框的 checked 属性,然后执行 .click() 如果它与我想要的不匹配,但这听起来不是很优雅。
回答by Zsolt
Try this one:
试试这个:
$('#locked').removeAttr('checked');
It's just a guess to your case, but usually works for me like a charm.
这只是对您的情况的猜测,但通常对我来说就像一种魅力。
EDIT: Taking a look at jQueryUI documentation, here is a method you should also try after changing programatically the state of the checkbox:
编辑:查看 jQueryUI 文档,这里有一种方法,您还应该在以编程方式更改复选框的状态后尝试:
$('#locked').button( "refresh" )
"Refreshes the visual state of the button. Useful for updating button state after the native element's checked or disabled state is changed programatically."
“刷新按钮的视觉状态。用于在以编程方式更改本机元素的选中或禁用状态后更新按钮状态。”
回答by Blazemonger
jQuery 1.5.x and earlier: $('#locked').attr('checked','');
jQuery 1.5.x 及更早版本: $('#locked').attr('checked','');
jQuery 1.6.0 and later: $('#locked').prop('checked', false);
jQuery 1.6.0 及更高版本: $('#locked').prop('checked', false);
The checked
attribute is considered a property, and has its own method now. You should use .prop()
if it's available to ensure the desired behavior is observed by your users.
该checked
属性被认为是一个属性,现在有自己的方法。.prop()
如果可以确保您的用户观察到所需的行为,您应该使用它。
回答by SaiKiran Mandhala
Use the new .prop()function in jQuery 1.6+:
在jQuery 1.6+ 中使用新的.prop()函数:
$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);
or with id
或带身
$('#myCheckboxId').prop('checked', false);
For jQuery 1.5 and below
对于jQuery 1.5 及以下
$('.myCheckbox').attr('checked','checked');
$('.myCheckbox').removeAttr('checked');
回答by twhitney
In newer versions of JQueryUI the checkboxradio()
function must be used with the refresh
option like so:
在较新版本的 JQueryUI 中,该checkboxradio()
函数必须与如下refresh
选项一起使用:
$("#selector").checkboxradio("refresh");
in order to update the checkbox visually after .prop("checked", true);
or .prop("checked", false);
has been used to check or uncheck the checkbox itself.
为了在.prop("checked", true);
或.prop("checked", false);
已用于检查或取消选中复选框本身之后,以视觉方式更新复选框。
Source: The API documentation: http://api.jqueryui.com/checkboxradio/
来源:API 文档:http: //api.jqueryui.com/checkboxradio/
refresh()
Refreshes the visual state of the widget. Useful for updating after the native element's checked or disabled state is changed programmatically.
刷新()
刷新小部件的视觉状态。用于在以编程方式更改本机元素的选中或禁用状态后进行更新。