Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过编程方式选中/取消选中jquery ui checkboxradio_Javascript_Jquery_Jquery Ui_Checkbox - Fatal编程技术网

Javascript 如何通过编程方式选中/取消选中jquery ui checkboxradio

Javascript 如何通过编程方式选中/取消选中jquery ui checkboxradio,javascript,jquery,jquery-ui,checkbox,Javascript,Jquery,Jquery Ui,Checkbox,我试图通过编程方式(即不使用鼠标)选中/取消选中jqueryuicheckboxradio小部件。我尝试过各种各样的事情,但没有快乐。要重现该问题,请选择一个空白html页面,其中只包含jquery和jquery ui脚本标记,然后动态创建小部件: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Blank</title&g

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

<!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>
默认情况下,复选框未选中。让我们试着检查一下:

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

这令人恼火。有人能解决这个看似简单的问题吗?

您需要调用
change
函数,因为
jquery ui
将更改更改时的视觉效果,因此仅设置
checked
属性不会产生任何效果

$("#chkbox").attr("checked","checked").change();

我希望它能帮助你,谢谢。
$container=$(“”)
$container.append($(“”)
$container.append($(“”)
$container.appendTo('body'))
$('#chkbox')。checkboxradio();
$('chkbox').prop('checked',true)。checkboxradio('refresh')

空白
该小部件有refresh()方法。此外,jQuery规范还指出,您应该使用prop()方法设置属性,如选中、选中或禁用
$("#chkbox").attr("checked","checked").change();