I have a page with a form on it. I would like to make all the checkboxes and radio buttons on the page read-only (non-editable) without making them have the "disabled"开发者_运维问答 appearance. I just want to prevent the user from easily changing the value of the checkbox. How can this be done?
This is probably the cleanest way to do it:
var deactivator = function(event){ event.preventDefault(); };
$(':radio, :checkbox').click(deactivator);
And to re-enable them:
$(':radio, :checkbox').unbind('click',deactivator);
You can use preventDefault
in the click event handler for all checkboxes and radio buttons:
$(":checkbox, :radio").click(function(e) {
e.preventDefault();
});
Here's a working example.
If you requirement is only to enable or disable the ability to interact with a specific check box, while not preventing posting of the element another solution is :
if (interaction === true){
$('#elID').click(false);
} else {
$('#elID').unbind('click');
}
精彩评论