Setting a checkbox based on the button pushed in a jQueryUI Dialog
I have a checkbox that when gets clicked will open up a dialog. If "Done" is clicked, I want the checkbox to have a checkmark. If "Cancel" is clicked, I want the checkbox not to have a checkmark.
Currently, I cannot get the checkbox to be set while using this code:
<script type="text/javascript">
$(document).ready(function() {
var dialog = $("#test-dlg").dialog({
modal:true,
autoOpen:false,
buttons: {
"Done": function() { $("#test-chk").attr("checked", "checked"); $(this).dialog("close"); },
"Cancel": function() { $("#text-chk").removeAttr("checked"); $(this).dialog("close"); }
}
});
$("#test-chk").click(function(e) {
dialog.dialog('open');
e.preventDefault();
开发者_如何学运维 });
});
</script>
</head>
<body>
<form action="#">
<input type="checkbox" id="test-chk" /><label for="test-chk">Testing</label>
</form>
</body>
<div id="test-dlg">
<p>Test Dialog</p>
</div>
You are using jQuery 1.6, so you need to use .prop()
instead of .attr()
.
change this: $("#test-chk").attr("checked", "checked");
to this: $("#test-chk").prop("checked", "checked");
精彩评论