Use js to implement an editable select drop-down list

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Select id = "name" name = "name"
Onkeydown = "clearSelect (this, event );"
Onkeypress = "writeSelect (this, event);" style = "width: 70px;">
<Option value = ""> </option>
<Option value = "test1"> test1 </option>
<Option value = "test2"> test2 </option>
<Option value = "test3"> test3 </option>
</Select>

<Script>
Function clearSelect (obj, e)
{
Opt = obj. options [0];
Opt. selected = "selected ";
If (e. keyCode = 8) | (e. charCode = 8) // use the backspace key to edit a verbatim deletion function
{
Opt. value = opt. value. substring (0, opt. value. length> 0? Opt. value. length-1: 0 );
Opt. text = opt. value;
}
If (e. keyCode = 46) | (e. charCode = 46) // use the Delete key to edit a word-by-word deletion.
{
Opt. value = "";
Opt. text = opt. value;
}
// Other buttons can be responded
}

Function writeSelect (obj, e)
{
Opt = obj. options [0];
Opt. selected = "selected ";
Opt. value + = String. fromCharCode (e. charCode | e. keyCode );
Opt. text = opt. value;
}
Function forbidBackSpace () // to prevent the Back-to-Previous Page function of backspace from conflicting with the Edit function of the drop-down box in IE, you must disable the backspace function. ForbidBackSpace can be written in <body onkeydown = "forbidBackSpace ();">.
{
If (event. keyCode = 8) & (event. srcElement. type! = "Text" & event. srcElement. type! = "Textarea" & event. srcElement. type! = "Password "))
{
Event. keyCode = 0;
Event. returnValue = false;
}
}
</Script>

Related Article

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.