This article describes the method of implementing the Select drop-down box with input function in js. Share it for your reference. The specific implementation method is as follows:
Implementation method one
Copy the code as follows: <HTML>
<HEAD>
<META http-equiv='Content-Type' content='text/html; charset=gb2312'>
<TITLE>js implements inputable drop-down box</TITLE>
</HEAD>
<BODY>
<div style="position:relative;">
<span style="margin-left:100px;width:18px;overflow:hidden;">
<select onchange="this.parentNode.nextSibling.value=this.value">
<option value="Germany">Germany</option>
<option value="Norway">Norway</option>
<option value="Switzerland"> Switzerland</option>
</select></span><input name="box">
</div>
</BODY>
</HTML>
Implementation method two
Copy the code code as follows: <select id="select" onkeydown="Select.del(this,event)" onkeypress="Select.write(this,event)">
<option value=""></option>
<option value="aaa">aaa</option>
<option value="bbb">bbb</option>
<option value="ccc">ccc</option>
</select>
<input type="button" value="get selection value" id="test" onclick="test();"/>
<script>
var Select = {
del : function(obj,e){
if((e.keyCode||e.which||e.charCode) == 8){
var opt = obj.options[0];
opt.text = opt.value = opt.value.substring(0, opt.value.length>0?opt.value.length-1:0);
}
},
write: function(obj,e){
if((e.keyCode||e.which||e.charCode) == 8)return;
var opt = obj.options[0];
opt.selected = "selected";
opt.text = opt.value += String.fromCharCode(e.charCode||e.which||e.keyCode);
}
}
function test(){
alert(document.getElementById("select").value);
}
</script><br />
I hope this article will be helpful to everyone's JavaScript programming.