mardi 13 décembre 2016

How to Check/Uncheck jquery-ui checkboxradio programatically

I am trying to check/uncheck a jquery-ui checkboxradio widget programatically (i.e. without using the mouse). I've tried all manner of things but not joy. To reproduce the issue, choose a blank html page with just jquery and jquery-ui script tags and then create the widget dynamically:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blank</title>
    <link rel="stylesheet" href="http://ift.tt/2gYNFtr">
    <script src="http://ift.tt/20g0BuL"></script>
    <script src="http://ift.tt/2dsPzTA"></script>
</head>
<body>
</body>
</html>

After the page loads, I hit F12 to bring up dev-tools and enter the following commands to get a minimal jquery-ui checkbox:

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

The checkbox is unchecked by default. Lets try to check it:

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

This is irritating. Does anyone have a solution to this seemingly simple problem?




Aucun commentaire:

Enregistrer un commentaire