Can HTML checkboxes be set to readonly?

前端 未结 30 2062
無奈伤痛
無奈伤痛 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 08:59

    If you want ALL your checkboxes to be "locked" so user can't change the "checked" state if "readonly" attibute is present, then you can use jQuery:

    $(':checkbox').click(function () {
        if (typeof ($(this).attr('readonly')) != "undefined") {
            return false;
        }
    });
    

    Cool thing about this code is that it allows you to change the "readonly" attribute all over your code without having to rebind every checkbox.

    It works for radio buttons as well.

提交回复
热议问题