以编程方式取消选中 jQuery UI 复选框按钮
我的页面上有一个 HTML 复选框元素,如下所示:
<input type="checkbox" id="locked" /><label for="locked">Locked</label>
我在 $(document).ready() 内部进行调用,将其更改为 jQuery UI 复选框按钮,如下所示:
$('#locked').button({
'icons': {
'primary': 'ui-icon-unlocked'
},
'label': 'Unlocked'
});
背景上下文是用户可以使用此按钮锁定/解锁特定实体,以便后台进程不会更改它,并且它以“解锁”状态开始。如果 JavaScript 未打开,用户会看到一个复选框及其旁边的“锁定”标签。
我希望能够以编程方式选中/取消选中此复选框按钮。我已经尝试过:
$('#locked').attr('checked', false);
但是复选框按钮不会更新以反映底层控件的选中状态。
我可以测试复选框的选中属性,然后如果它与我想要的不匹配,则执行 .click() ,但这听起来不太优雅。
I have an HTML checkbox element on my page like so:
<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:
$('#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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
这只是对你的情况的猜测,但通常对我来说就像一个魅力。
编辑:查看 jQueryUI 文档,这里是您在以编程方式更改复选框状态后还应该尝试的方法:
“刷新按钮的视觉状态。对于在更改本机元素的选中或禁用状态后更新按钮状态很有用以编程方式。”
Try this one:
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:
"Refreshes the visual state of the button. Useful for updating button state after the native element's checked or disabled state is changed programatically."
jQuery 1.5.x 及更早版本:
$('#locked').attr('checked','');
jQuery 1.6.0 及更高版本:
$('#locked')。 prop('checked', false);
checked
属性被视为属性,并且现在有自己的方法。如果可以使用.prop()
,您应该使用它来确保用户观察到所需的行为。jQuery 1.5.x and earlier:
$('#locked').attr('checked','');
jQuery 1.6.0 and later:
$('#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.在 jQuery 1.6+ 中使用新的 .prop() 函数:
或使用 id
对于 jQuery 1.5 及以下版本
Use the new .prop() function in jQuery 1.6+:
or with id
For jQuery 1.5 and below
在较新版本的 JQueryUI 中,
checkboxradio()
函数必须与refresh
选项一起使用,如下所示:以便在
.prop("checked ", true);
或.prop("checked", false);
用于选中或取消选中复选框本身。资料来源:API 文档:http://api.jqueryui.com/checkboxradio/
In newer versions of JQueryUI the
checkboxradio()
function must be used with therefresh
option like so: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.Source: The API documentation: http://api.jqueryui.com/checkboxradio/