select只读属性设置的常用方案

来源:互联网 发布:js按钮点击事件隐藏 编辑:程序博客网 时间:2024/06/05 20:20

因为Select下拉框只支持disabled属性,不支持readOnly属性,而在提交时,disabled的控件,又是不提交值的。现提供以下几种解决方案:

1.使用js文件

<select id="select">  
    <option>bbb</option>  
    <option>aaa</option>
    <option selected>ccc</option>   
</select>  
<script type="text/javascript">  
SetReadOnly(document.getElementById("select"));  
    function SetReadOnly(obj){  
        if(obj){  
            obj.onbeforeactivate = function(){return false;};  
            obj.onfocus = function(){obj.blur();};  
            obj.onmouseover = function(){obj.setCapture();};  
            obj.onmouseout = function(){obj.releaseCapture();};  
        }  
    }  
</script>


2.使用jquery方式解决


 $(function(){
          $("select").attr("disabled", "disabled");  
          //如果和jquery1.6以上版本,可以使用以下语句:
          $("select").prop("disabled", true);
    });

3.页面载入时,设置只读,表单提交时移除disabled

//两种方法

$("#selectone").attr("disabled",true);

$("#selectone").attr("disabled","disabled");

//移除disabled

$("#selectone").attr("disabled",false);

$("#selectone").attr("disabled","");

$("#selectone").removeAttr("disabled");




原创粉丝点击