During project development, we often encounter the drop-down box to be readonly, but unfortunately, select does not have readonly attributes, so we need to include a span outside the select and change it through js.
The following html code adds a span tag to the pull-down tag of struts2, which makes the drop-down box unreadable when the page is loaded.
The code copy is as follows:
<body onload="init()">
<span id="id_select">
<s:select name="sjdwmc" list="sjdxdwList" listKey="dxbh" listValue="dwmc" headerKey="" headerValue=""></s:select>
</span>
</body>
The following is the js code, call selectReadOnly in the init method to make the drop-down box read-only.
The code copy is as follows:
/*Set select to read-only according to the id of span on the page/
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");
}
The work is done here and try the effect! ! !