console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
input, button {
padding: 5px;
margin-top: 5px;
margin-bottom: 10px;
}
.input-container {
display: grid;
width: 200px;
align-items: center;
overflow: hidden;
}
.input-container input {
padding: 5px;
margin-top: 5px;
margin-bottom: 10px;
}
.input-container button {
position: fixed;
justify-self: end;
background-color: lightgray;
color: white;
border: none;
border-radius: 50%;
cursor: pointer;
width: 20px;
height: 20px;
}
.input-container button:hover {
background-color: gray;
}
.custom-select {
position: relative;
display: inline-block;
left: -10px;
}
.custom-select select {
padding:10px 0px 10px 30px;
font-size: 16px;
border: none;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
background-color: transparent;
background-image: url('https://img.picui.cn/free/2025/06/19/6853c08bec172.png');
background-repeat: no-repeat;
background-position: left 10px center;
background-size: 16px 16px;
}
.custom-select::after {
content: "";
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
width: 16px;
height: 16px;
background-image: url('https://img.picui.cn/free/2025/06/19/6853c08bec172.png');
background-size: cover;
background-position: center;
pointer-events: none;
}
</style>
</head>
<body>
<div class="custom-select">
<select id="mySelect" onchange="updateInputWithSecondValue()">
<option value="" disabled selected hidden>请选择房间号</option>
<option data-value1="A幢101" data-value2="杭州吉租科技有限公司">A幢101</option>
<option data-value1="B幢101" data-value2="浙江豆办网络科技有限公司">B幢100010</option>
<option data-value1="C幢101" data-value2="浙江日丽能源管理有限公司">C幢101</option>
</select>
</div>
<div class="input-container">
<input type="text" id="searchInput" placeholder="请输入公司名称/房间号" onkeyup="setSelectedOptionByInput()">
<button id="resetButton" onclick="resetInputs()" style="display:none;">×</button>
</div>
<script>
function setSelectedOptionByValue(value) {
var options = document.querySelectorAll('#mySelect option');
options.forEach(function(option) {
if (option.getAttribute('data-value1') === value || option.getAttribute('data-value2') === value) {
document.getElementById('mySelect').value = option.value;
document.getElementById('searchInput').value = option.getAttribute('data-value2') || '';
toggleResetButton();
adjustWidth();
}
});
}
function setSelectedOptionByInput() {
var inputValue = document.getElementById('searchInput').value.trim();
setSelectedOptionByValue(inputValue);
}
function updateInputWithSecondValue() {
var selectedOption = document.getElementById('mySelect').selectedOptions[0];
if (selectedOption) {
document.getElementById('searchInput').value = selectedOption.getAttribute('data-value2') || '';
toggleResetButton();
adjustWidth();
}
}
function resetInputs() {
document.getElementById('mySelect').value = '';
document.getElementById('searchInput').value = '';
toggleResetButton();
document.getElementById('mySelect').style.width = '156px';
}
function toggleResetButton() {
var searchInput = document.getElementById('searchInput');
var resetButton = document.getElementById('resetButton');
if (searchInput.value.trim() !== '') {
resetButton.style.display = 'inline';
} else {
resetButton.style.display = 'none';
}
}
function adjustWidth() {
const selectElement = document.getElementById('mySelect');
const tempSpan = document.createElement('span');
tempSpan.style.fontSize = getComputedStyle(selectElement).fontSize;
tempSpan.style.fontFamily = getComputedStyle(selectElement).fontFamily;
tempSpan.textContent = selectElement.options[selectElement.selectedIndex].text;
document.body.appendChild(tempSpan);
const width = tempSpan.offsetWidth + 60;
document.body.removeChild(tempSpan);
selectElement.style.width = `${width}px`;
}
document.addEventListener('DOMContentLoaded', function() {
adjustWidth();
document.getElementById('mySelect').addEventListener('change', adjustWidth);
});
document.getElementById('searchInput').addEventListener('input', toggleResetButton);
</script>
</body>
</html>