select下拉框一般来说只能进行选择,然而很多应用场景需要支持既能选择也能输入的功能。下边是自己整理的html和js代码片段。
<!--HTML片段-->
<td>
<c:forEach items="${qt009List }" var="list">
<select id="WPAYCARDNO" onkeydown="Select.del(this,event)" onkeypress="Select.write(this,event)">
<option>${list.c_015 }</option>
</select>
</c:forEach>
</td>
//js代码片段
<script type="text/javascript">
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);
}
}
</script>