Use JS to set the Drop-down box for read-only tips _javascript tips

Source: Internet
Author: User
In the project development process we often encounter to set the Drop-down box read-only (ReadOnly), but unfortunately the select has no read-only attribute, so you need to include a span outside the select, through JS to change.

The following HTML code adds a span tag to the STRUTS2 drop-down tab and makes the dropdown box unreadable when the page is loaded.
Copy Code code as follows:

<body onload= "init ()" >
<span id= "Id_select" >
<s:select name= "SJDWMC" list= "sjdxdwlist" listkey= "Dxbh" listvalue= "DWMC" "headerkey=" "headervalue=: Select>
</span>
</body>

The following is the JS code, called Selectreadonly in the Init method to make the Drop-down box read-only.
Copy Code code as follows:

/* Set Select to read-only based on 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");
}

Do this here, try the effect of it!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.