开发者

Not-disabled Read-only checkbox in Jquery?

开发者 https://www.devze.com 2023-04-05 04:39 出处:网络
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 j

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');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消