以编程方式取消选中 jQuery UI 复选框按钮

发布于 2024-12-04 04:44:56 字数 646 浏览 3 评论 0原文

我的页面上有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

习ぎ惯性依靠 2024-12-11 04:44:56

试试这个:

$('#locked').removeAttr('checked');

这只是对你的情况的猜测,但通常对我来说就像一个魅力。

编辑:查看 jQueryUI 文档,这里是您在以编程方式更改复选框状态后还应该尝试的方法:

$('#locked').button( "refresh" )

“刷新按钮的视觉状态。对于在更改本机元素的选中或禁用状态后更新按钮状态很有用以编程方式。”

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:

$('#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."

も星光 2024-12-11 04:44:56

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.

你的心境我的脸 2024-12-11 04:44:56

jQuery 1.6+ 中使用新的 .prop() 函数:

$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);

或使用 id

$('#myCheckboxId').prop('checked', false);

对于 jQuery 1.5 及以下版本

$('.myCheckbox').attr('checked','checked');
$('.myCheckbox').removeAttr('checked');

Use the new .prop() function in jQuery 1.6+:

$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);

or with id

$('#myCheckboxId').prop('checked', false);

For jQuery 1.5 and below

$('.myCheckbox').attr('checked','checked');
$('.myCheckbox').removeAttr('checked');
叹沉浮 2024-12-11 04:44:56

在较新版本的 JQueryUI 中,checkboxradio() 函数必须与 refresh 选项一起使用,如下所示:

$("#selector").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 the refresh option like so:

$("#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.

Source: The API documentation: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文