王多静

这里是我的记事本

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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>
复制代码

 

posted on   王多静  阅读(397)  评论(0编辑  收藏  举报

编辑推荐:
· 基于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最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示