Tips for setting the drop down box as read only with js


In the process of project development, we often encounter the need to set the drop-down box as readonly, but unfortunately, select does not have a read-only property, so we need to include a span outside the select, which is changed by js.

The following HTML code adds a span tag to the struts2 drop down to make the drop down unreadable when the page loads.


<body onload="init()">
<span id="id_select">
<s:select name="sjdwmc" list="sjdxdwList" listKey="dxbh" listValue="dwmc" headerKey="" headerValue=""></s:select>
</span>
</body>

Here is the js code that calls selectReadOnly in the init method to make the drop-down box read only.


/* According to the page span the id Set up the select As read-only /

function selectReadOnly(selectedId){
var obj = document.getElementById(selectedId);
obj.onmouseover = function(){
obj.setCapture();
}
obj.onmouseout = function(){
obj.releaseCapture();
}
obj.onfocus = function(){
obj.blur();
}
obj.onbeforeactivate = function(){
return false;
}
}

function init(){
selectReadOnly("id_select");
}

Do here is done, try the effect!!