Can HTML checkboxes be set to readonly?

前端 未结 30 1680
無奈伤痛
無奈伤痛 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:03

    Some of the answers on here seem a bit roundabout, but here's a small hack.

    <form id="aform" name="aform" method="POST">
        <input name="chkBox_1" type="checkbox" checked value="1" disabled="disabled" />
        <input id="submitBttn" type="button" value="Submit" onClick='return submitPage();'>
    </form>​
    

    then in jquery you can either choose one of two options:

    $(document).ready(function(){
        //first option, you don't need the disabled attribute, this will prevent
        //the user from changing the checkbox values
        $("input[name^='chkBox_1']").click(function(e){
            e.preventDefault();
        });
    
        //second option, keep the disabled attribute, and disable it upon submit
        $("#submitBttn").click(function(){
            $("input[name^='chkBox_1']").attr("disabled",false);
            $("#aform").submit();
        });
    
    });
    

    demo: http://jsfiddle.net/5WFYt/

    0 讨论(0)
  • 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;
           }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:04

    I just don't want the client to be able to change them under certain circumstances.

    READONLY itself won't work. You may be able to do something funky w/CSS but we usually just make them disabled.

    WARNING: If they're posted back then the client can change them, period. You can't rely on readonly to prevent a user from changing something. The could always use fiddler or just chane the html w/firebug or some such thing.

    0 讨论(0)
  • 2020-11-22 09:05

    This is a checkbox you can't change:

    <input type="checkbox" disabled="disabled" checked="checked">
    

    Just add disabled="disabled" as an attribute.


    Edit to address the comments:

    If you want the data to be posted back, than a simple solutions is to apply the same name to a hidden input:

    <input name="myvalue" type="checkbox" disabled="disabled" checked="checked"/>
    <input name="myvalue" type="hidden" value="true"/>
    

    This way, when the checkbox is set to 'disabled', it only serves the purpose of a visual representation of the data, instead of actually being 'linked' to the data. In the post back, the value of the hidden input is being sent when the checkbox is disabled.

    0 讨论(0)
  • 2020-11-22 09:05

    Very late to the party but I found an answer for MVC (5) I disabled the CheckBox and added a HiddenFor BEFORE the checkbox, so when it is posting if finds the Hidden field first and uses that value. This does work.

     <div class="form-group">
         @Html.LabelFor(model => model.Carrier.Exists, new { @class = "control-label col-md-2" })
             <div class="col-md-10">
                  @Html.HiddenFor(model => model.Carrier.Exists)
                  @Html.CheckBoxFor(model => model.Carrier.Exists, new { @disabled = "disabled" })
                  @Html.ValidationMessageFor(model => model.Carrier.Exists)
              </div>
     </div>
    
    0 讨论(0)
  • 2020-11-22 09:06

    Most of the current answers have one or more of these problems:

    1. Only check for mouse not keyboard.
    2. Check only on page load.
    3. Hook the ever-popular change or submit events which won't always work out if something else has them hooked.
    4. Require a hidden input or other special elements/attributes that you have to undo in order to re-enable the checkbox using javascript.

    The following is simple and has none of those problems.

    $('input[type="checkbox"]').on('click keyup keypress keydown', function (event) {
        if($(this).is('[readonly]')) { return false; }
    });
    

    If the checkbox is readonly, it won't change. If it's not, it will. It does use jquery, but you're probably using that already...

    It works.

    0 讨论(0)
提交回复
热议问题