Can HTML checkboxes be set to readonly?

前端 未结 30 2054
無奈伤痛
無奈伤痛 2020-11-22 08:39

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

30条回答
  •  迷失自我
    2020-11-22 09:04

    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;
           }
        }
    }
    

提交回复
热议问题