Using JS to implement an editable select Drop-down list _javascript tips

Source: Internet
Author: User
Tags delete key
Copy Code code 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 (BACKSPACE) key to implement the edit function for verbatim deletion
{
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 implement the edit feature for verbatim deletion
{
Opt.value = "";
Opt.text = Opt.value;
}
You can also implement the response of other keystrokes
}

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 ()//For in IE, to avoid backspace return to the previous page function, and this drop-down box edit function conflict, need to ban the function of backspace. Forbidbackspace can be written in <body onkeydown= "Forbidbackspace ();" > in.
{
if ((Event.keycode = 8) && (event.srcElement.type!= "text" && event.srcElement.type!= "textarea" && Amp Event.srcElement.type!= "password"))
{
Event.keycode = 0;
Event.returnvalue = false;
}
}
</script>

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.