【1】Html+CSS兼容IE实现
基本原理:使用 input/select 两个元素,然后 input 覆盖在 select 上层 ,宽度并比select窄一点,这样用户就能点到select的箭头弹出下拉框
<div class="select-editable">
<select onchange="this.nextElementSibling.value=this.value">
<option value=""></option>
<option value="115x175 mm">115x175 mm</option>
<option value="120x160 mm">120x160 mm</option>
<option value="120x287 mm">120x287 mm</option>
</select>
<input type="text" name="format" value="" />
</div>
.select-editable {
position:relative;
background-color:white;
border:solid grey 1px;
width:120px;
height:18px;
}
.select-editable select {
position:absolute;
top:0px;
left:0px;
font-size:14px;
border:none;
width:120px;
margin:0;
}
.select-editable input {
position:absolute;
top:0px;
left:0px;
width:100px;
padding:1px;
font-size:12px;
border:none;
}
.select-editable select:focus, .select-editable input:focus {
outline:none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.select-editable {
position: relative;
background-color: white;
border: solid grey 1px;
width: 120px;
height: 18px;
}
.select-editable select {
position: absolute;
top: 0px;
left: 0px;
font-size: 14px;
border: none;
width: 120px;
margin: 0;
}
.select-editable input {
position: absolute;
top: 0px;
left: 0px;
width: 100px;
padding: 1px;
font-size: 12px;
border: none;
}
.select-editable select:focus,
.select-editable input:focus {
outline: none;
}
</style>
<script src="./jquery.min.js"></script>
</head>
<body>
<div class="select-editable">
<select onchange="this.nextElementSibling.value=this.value" id="cookies">
<!-- <option value=""></option>
<option value="115x175 mm">115x175 mm</option>
<option value="120x160 mm">120x160 mm</option>
<option value="120x287 mm">120x287 mm</option> -->
</select>
<input type="text" name="format" value="" />
</div>
<script>
window.onload = function () {
var optionList = [
{ value: '1' },
{ value: '2' },
{ value: '3' },
{ value: '4' },
{ value: '5' },
]
$('#cookies').empty()
if (optionList && optionList.length > 0) {
for (var i = 0; i < optionList.length; i++) {
$('#cookies').append(`<option value="` + optionList[i].value + `">`+optionList[i].value+`</option>`);
}
}
}
</script>
</body>
</html>
【2】datalist
Datalist是HTML5的原生标签,用于向input提供下拉和自动提示选项,支持输入和下拉选择两种方式。但是目前IE10及其以下浏览器并不支持, 并且IE11无法触发input和change事件,相当于IE系列均无法很好地实现。如果仅支持Chrome或Eletron桌面的应用则可以放心使用。文章来源:https://www.toymoban.com/news/detail-732393.html
<input list="cookies" placeholder="Type of Cookie"/>
<datalist id="cookies">
<option value="Chocolate Chip"/>
<option value="Peanut Butter"/>
<option value="Raisin Oatmeal"/>
</datalist>
文章来源地址https://www.toymoban.com/news/detail-732393.html
<body>
<div>
<input list="cookies" placeholder="请选择或输入" />
<datalist id="cookies">
<!-- <option value="Chocolate Chip" />
<option value="Peanut Butter" />
<option value="Raisin Oatmeal" /> -->
</datalist>
</div>
<script>
window.onload = function () {
var optionList = [
{ value: '1' },
{ value: '2' },
{ value: '3' },
{ value: '4' },
{ value: '5' },
]
$('#cookies').empty()
if (optionList && optionList.length > 0) {
for (var i = 0; i < optionList.length; i++) {
$('#cookies').append(`<option value="` + optionList[i].value + `" />`);
}
}
}
</script>
</body>
</html>
到了这里,关于【JavaScript】让select选择框可输入和可下拉选择:的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!