Can HTML checkboxes be set to readonly?

前端 未结 30 1694
無奈伤痛
無奈伤痛 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.

    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/

提交回复
热议问题