65.9K
CodeProject 正在变化。 阅读更多。
Home

带有可选项目的输入文本框(组合框)

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2016年4月15日

CPOL
viewsIcon

18439

downloadIcon

199

将文本框和选择框(ComboBox)组合到一个 HTML 表单中。

立即下载 ComboBox.zip

引言

该组件的开发是为了解决在表单中使用文本框的同时提供预定义选项的问题。

ComboBox

使用代码

诀窍是将使用“ul”列表构建的组合框与文本框结合起来,使用 CSS 来调整它们的大小,并使用少量 JavaScript 来完成剩下的工作。

HTML

    <div class="container">

        <div class="comboBox">
            <div class="comboGroup">
                <input type="text" id="carBrand" placeholder="Select or type a new option..." />
                <ul>
                    <li>Honda</li>
                    <li>Mazda</li>
                    <li>Nissan</li>
                    <li>Toyota</li>
                </ul>
            </div>
            <script>new comboBox('carBrand');</script>
        </div>

    </div>

CSS

    <style>

        .container {
            padding: 50px;
        }
             
        *:focus {
            outline: none;
        }

        .comboBox {
            width: 400px;
        }

        .comboGroup {
            position: relative;
            width: 100%;
            text-align: left;
        }

            .comboGroup * {
                font-family: arial, helvetica, sans-serif;
                font-size: 14px;
            }

            .comboGroup input {
                padding: 8px;
                margin: 0;
                border: 1px solid #ccc;
                border-radius: 4px;
                width: 100%;
                background-color: #fff;
                z-index: 0;
            }

            .comboGroup ul {
                padding: 8px 20px 8px 8px;
                margin: 0;
                border: 1px solid #ccc;
                border-radius: 4px;
                width: 100%;
                background-color: #fefefe;
                z-index: 1;
                position: absolute;
                top: 40px;
                display: none;
            }

            .comboGroup li {
                padding: 6px;
                margin: 0;
                border-radius: 4px;
                width: 100%;
                display: block;
            }

                .comboGroup li:hover {
                    cursor: pointer;
                    background-color: #f5f5f5;
                }

    </style>

Javascript

    <script>

        function comboBox(id) {
            var box = this;
            box.txt = document.getElementById(id);
            box.hasfocus = false;
            box.opt = -1;
            box.ul = box.txt.nextSibling;
            while (box.ul.nodeType == 3) box.ul = box.ul.nextSibling;
            box.ul.onmouseover = function () {
                box.ul.className = '';
            };
            box.ul.onmouseout = function () {
                box.ul.className = 'focused';
                if (!box.hasfocus) box.ul.style.display = 'none';
            };
            box.list = box.ul.getElementsByTagName('li');
            for (var i = box.list.length - 1; i >= 0; i--) {
                box.list[i].onclick = function () {
                    box.txt.value = this.firstChild.data;
                }
            }
            box.txt.onfocus = function () {
                box.ul.style.display = 'block';
                box.ul.className = 'focused';
                box.hasfocus = true;
                box.opt = -1;
            };
            box.txt.onblur = function () {
                box.ul.className = '';
                box.hasfocus = false;
            };
            box.txt.onkeyup = function (e) {
                box.ul.style.display = 'none';
                var k = (e) ? e.keyCode : event.keyCode;
                if (k == 40 || k == 13) {
                    if (box.opt == box.list.length - 1) {
                        box.opt = -1;
                    }
                    box.txt.value = box.list[++box.opt].firstChild.data;
                } else if (k == 38 && box.opt > 0) {
                    box.txt.value = box.list[--box.opt].firstChild.data;
                }
                return false;
            };
            box.ul.onclick = function (e) {
                box.ul.style.display = 'none';
                return false;
            };
        }

    </script>

关注点

该组件非常易于用于任何 HTML 表单!无需 Bootstrap 或 jQuery 依赖。

历史

暂无更新。

© . All rights reserved.