HTML form readonly SELECT tag/input

前端 未结 30 2944
陌清茗
陌清茗 2020-11-22 08:28

According to HTML specs, the select tag in HTML doesn\'t have a readonly attribute, only a disabled attribute. So if you want to keep

30条回答
  •  旧巷少年郎
    2020-11-22 08:54

    Following on from Grant Wagners suggestion; here is a jQuery snippet that does it with handler functions instead of direct onXXX attributes:

    var readonlySelect = function(selector, makeReadonly) {
    
        $(selector).filter("select").each(function(i){
            var select = $(this);
    
            //remove any existing readonly handler
            if(this.readonlyFn) select.unbind("change", this.readonlyFn);
            if(this.readonlyIndex) this.readonlyIndex = null;
    
            if(makeReadonly) {
                this.readonlyIndex = this.selectedIndex;
                this.readonlyFn = function(){
                    this.selectedIndex = this.readonlyIndex;
                };
                select.bind("change", this.readonlyFn);
            }
        });
    
    };
    

提交回复
热议问题