Can HTML checkboxes be set to readonly?

2018-12-31 10:28发布

I thought they could be, but as I'm not putting my money where my mouth was (so to speak) setting the readonly attribute doesn't actually seem to do anything.

I'd rather not use Disabled, since I want the checked check boxes to be submitted with the rest of the form, I just don't want the client to be able to change them under certain circumstances.

标签: html checkbox
30条回答
萌妹纸的霸气范
2楼-- · 2018-12-31 11:07

This presents a bit of a usability issue.

If you want to display a checkbox, but not let it be interacted with, why even a checkbox then?

However, my approach would be to use disabled (The user expects a disabled checkbox to not be editable, instead of using JS to make an enabled one not work), and add a form submit handler using javascript that enables checkboxes right before the form is submitted. This way you you do get your values posted.

ie something like this:

var form = document.getElementById('yourform');
form.onSubmit = function () 
{ 
    var formElems = document.getElementsByTagName('INPUT');
    for (var i = 0; i , formElems.length; i++)
    {  
       if (formElems[i].type == 'checkbox')
       { 
          formElems[i].disabled = false;
       }
    }
}
查看更多
一个人的天荒地老
3楼-- · 2018-12-31 11:07

The main reason people would like a read-only check-box and (as well) a read-only radio-group is so that information that cannot be changed can be presented back to the user in the form it was entered.

OK disabled will do this -- unfortunately disabled controls are not keyboard navigable and therefore fall foul of all accessibility legislation. This is the BIGGEST hangup in HTML that I know of.

查看更多
公子世无双
4楼-- · 2018-12-31 11:08

I happened to notice the solution given below. In found it my research for the same issue. I don't who had posted it but it wasn't made by me. It uses jQuery:

$(document).ready(function() {
    $(":checkbox").bind("click", false);
});

This would make the checkboxes read only which would be helpful for showing readonly data to the client.

查看更多
长期被迫恋爱
5楼-- · 2018-12-31 11:08

Just use simple disabled tag like this below.

<input type="checkbox" name="email"  disabled>
查看更多
弹指情弦暗扣
6楼-- · 2018-12-31 11:12

I used this to achieve the results:

<input type=checkbox onclick="return false;" onkeydown="return false;" />
查看更多
倾城一夜雪
7楼-- · 2018-12-31 11:12
onclick="javascript: return false;"
查看更多
登录 后发表回答