javascript - 如何以编程方式检查/取消选中 jquery-ui checkboxradio

标签 javascript jquery jquery-ui checkbox

我正在尝试以编程方式选中/取消选中 jquery-ui checkboxradio 小部件(即不使用鼠标)。我尝试过各种各样的事情,但没有快乐。要重现该问题,请选择一个只有 jqueryjquery-ui 脚本标签的空白 html 页面,然后动态创建小部件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blank</title>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
</body>
</html>

页面加载后,我按 F12 调出开发工具并输入以下命令以获得最小的 jquery-ui 复选框:

$container = $("<div>")
$container.append($("<label for='chkbox'>"))
$container.append($("<input type='checkbox' id='chkbox'>"))
$container.appendTo('body')
$('#chkbox').checkboxradio()

该复选框默认未选中。让我们尝试检查一下:

$('#chkbox').attr('checked', true) // nope
$('#chkbox').checkboxradio('refresh') // nope
$('#chkbox').attr('checked', false)
$('#chkbox').prop('checked', true) // nope
$('#chkbox').prop('checked', false)
$('#chkbox').addClass('ui-checkboxradio-checked') // not this either
$('#chkbox').removeClass('ui-checkboxradio-checked')
$('#chkbox').checkboxradio('option', 'classes.ui-checkboxradio-checked', true) // does nothing
$('#chkbox').checkboxradio('option', 'ui-checkboxradio-checked', true) // does nothing
$('#chkbox').checkboxradio('option', 'classes.ui-checkboxradio.ui-checkboxradio-checked', true) // and again, nothing...

这很烦人。有没有人能解决这个看似简单的问题?

最佳答案

希望对您有所帮助,谢谢。

$container = $("<div>")
$container.append($("<label for='chkbox'>"))
$container.append($("<input type='checkbox' id='chkbox' >"))
$container.appendTo('body')
 $('#chkbox').checkboxradio();
$('#chkbox').prop('checked',true).checkboxradio('refresh')
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blank</title>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
</body>
</html>

关于javascript - 如何以编程方式检查/取消选中 jquery-ui checkboxradio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41135295/

相关文章:

React 中的 jQuery UI Datepicker 抛出 Uncaught TypeError

javascript - 在 WebStorm 中为 node_modules 启用代码完成

javascript - 页面第二次加载时无法显示加载微调图像

javascript - 如何根据文本制作 if 语句

javascript - Angular 副本无法让客户端保持最新状态

javascript - 是否可以将点击事件添加到 Canvas 标签?

jQuery 在 5 秒后自动隐藏元素

javascript - Glassbox.js javascript 插件在 JQuery 中不起作用

javascript - If/Else if语句缩减

jquery - 为什么灯箱 jQuery 插件对我不起作用?