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

  • 2020-03-30 02:36:46
  • OfStack

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> 

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.onmouseout = function(){ 
obj.onfocus = function(){ 
obj.onbeforeactivate = function(){ 
return false; 

function init(){ 

Do here is done, try the effect!!

Related articles: