JQ可编辑的下拉框<select>
下面两个是函数不一样, 样式都一样 代码一全一点
代码一
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .brandname-box { width: 100px; height: 30px; position: relative; } .brandname-input { width: 78px; border: 0; height: 20px; line-height: 20px; padding: 3px 0; position: absolute; top: 2px; left: 2px; } .brandname-select { width: 100px; background-color: #FFF; border: 1px #ddd solid; cursor: pointer; height: 30px; left: 0px; position: absolute; top: 0px; } .input-init {
/*此class去除文本框的默认格式,如焦点聚焦的时候闪框*/
outline-color: #ddd; outline-offset: 0; outline-style: none; outline-width: 0; /*lighting-color: #fff;*/ /*手机端注意*/ } </style> </head> <body> <div class="brandname-box"> <select type="text" class="brandname-select input-init" id="brandnameSelect" onchange="changeBrand(this)"> <!--第一个隐藏 为了解决不让默认选中第一个--> <option style="display: none;"></option> <option value="65">小华</option> <option value="66">小红</option> <option value="67">小八</option> <option value="68">小何</option> </select> <input type="text" class="brandname-input input-init" id="brandnameInput" placeholder="请选择"> </div> <button id="handlesummit">确定</button> <script src="../jquery-3.4.1.min.js"></script> <script> var brandName = $('#brandnameInput'); function changeBrand(_this) { console.log(_this) // console.log('获取的id:' + _this.value, '获取的name:' + $('#brandnameSelect').find("option:selected").text()) if(this.value) { brandName .val($('#brandnameSelect').find("option:selected").text()); } else { this.value = ''; brandName .val('') } }; $("#handlesummit").click(function(){ console.log('获取的id:' + $('#brandnameSelect').val(), '获取的name:' + $('#brandnameSelect').find("option:selected").text()) }) </script> </body> </html>
代码二
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .brandname-box { width: 100px; height: 30px; position: relative; } .brandname-input { width: 78px; border: 0; height: 20px; line-height: 20px; padding: 3px 0; position: absolute; top: 2px; left: 2px; vertical-align: middle; } .brandname-select { width: 100px; background-color: #FFF; border: 1px #ddd solid; cursor: pointer; height: 30px; left: 0px; position: absolute; top: 0px; } .input-init { outline-color: #ddd; outline-offset: 0; outline-style: none; outline-width: 0; /*lighting-color: #fff;*/ /*手机端注意*/ } </style> </head> <body> <div class="brandname-box"> <select type="text" class="brandname-select input-init" id="brandnameSelect"> <option value="65">小华</option> <option value="66">小红</option> <option value="67">小八</option> <option value="68">小何</option> </select> <input type="text" class="brandname-input input-init" id="brandnameInput"> </div> <script src="../jquery-3.4.1.min.js"></script> <script> var brandName = $('#brandnameInput'); $('#brandnameSelect').change(function() { console.log('获取的id:' + this.value, '获取的name:' + $('#brandnameSelect').find("option:selected").text()) if(this.value) { brandName .val($('#brandnameSelect').find("option:selected").text()); } else { this.value = ''; brandName .val('') } }); </script> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通