jQuery製作簡潔的多級聯動Select下拉框

來源:互聯網
上載者:User

標籤:doc   query   mil   並且   中文   type   flow   repeat   htm   

今天我們要來分享一款很實用的jQuery外掛程式,它是一個基於jQuery多級聯動的省市地區Select下拉框,並且值得一提的是,這款聯動下拉框是經過自訂美化過的,外觀比瀏覽器內建的要漂亮許多。另外,這個Select下拉框也可以綁定下拉事件,並擷取當前選中項的值。

html代碼:

<div class="wrap">
        <div class="nice-select" name="nice-select">
            <input type="text" value="==選擇省份==" readonly>
            <ul>
                <li data-value="1">湖北省</li>
                <li data-value="2">廣東省</li>
                <li data-value="3">湖南省</li>
                <li data-value="4">四川省</li>
            </ul>
        </div>
        <div class="h20">
        </div>
        <div class="nice-select" name="nice-select">
            <input type="text" value="==選擇城市==" readonly>
            <ul>
                <li data-value="1">武漢市</li>
                <li data-value="2">深圳市</li>
                <li data-value="3">長沙市</li>
                <li data-value="4">成都市</li>
            </ul>
        </div>
        <div class="h20">
        </div>
        <div class="nice-select" name="nice-select">
            <input type="text" value="==選擇區縣==" readonly>
            <ul>
                <li data-value="1">蔡甸區</li>
                <li data-value="2">南山區</li>
                <li data-value="3">雨花區</li>
                <li data-value="4">武侯區</li>
            </ul>
        </div>
    </div>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script>
        $(‘[name="nice-select"]‘).click(function (e) {
            $(‘[name="nice-select"]‘).find(‘ul‘).hide();
            $(this).find(‘ul‘).show();
            e.stopPropagation();
        });
        $(‘[name="nice-select"] li‘).hover(function (e) {
            $(this).toggleClass(‘on‘);
            e.stopPropagation();
        });
        $(‘[name="nice-select"] li‘).click(function (e) {
            var val = $(this).text();
            var dataVal = $(this).attr("data-value");
            $(this).parents(‘[name="nice-select"]‘).find(‘input‘).val(val);
            $(‘[name="nice-select"] ul‘).hide();
            e.stopPropagation();
            alert("中文值是:" + val);
            alert("數字值是:" + dataVal);
            //alert($(this).parents(‘[name="nice-select"]‘).find(‘input‘).val());
        });
        $(document).click(function () {
            $(‘[name="nice-select"] ul‘).hide();
        });
    </script>

css代碼:

 body
        {
            color: #555;
            font-size: 14px;
            font-family: "微軟雅黑" , "Microsoft Yahei";
            background-color: #EEE;
        }
        a
        {
            color: #555;
        }
        a:hover
        {
            color: #f00;
        }
        input
        {
            font-size: 14px;
            font-family: "微軟雅黑" , "Microsoft Yahei";
        }
        .wrap
        {
            width: 500px;
            margin: 100px auto;
        }
        .h20
        {
            height: 20px;
            overflow: hidden;
            clear: both;
        }
        .nice-select
        {
            width: 245px;
            padding: 0 10px;
            height: 38px;
            border: 1px solid #999;
            position: relative;
            box-shadow: 0 0 5px #999;
            background: #fff url(images/a2.jpg) no-repeat right center;
            cursor: pointer;
        }
        .nice-select input
        {
            display: block;
            width: 100%;
            height: 38px;
            line-height: 38px \9;
            border: 0;
            outline: 0;
            background: none;
            cursor: pointer;
        }
        .nice-select ul
        {
            width: 100%;
            display: none;
            position: absolute;
            left: -1px;
            top: 38px;
            overflow: hidden;
            background-color: #fff;
            max-height: 150px;
            overflow-y: auto;
            border: 1px solid #999;
            border-top: 0;
            box-shadow: 0 3px 5px #999;
            z-index: 9999;
        }
        .nice-select ul li
        {
            height: 30px;
            line-height: 30px;
            overflow: hidden;
            padding: 0 10px;
            cursor: pointer;
        }
        .nice-select ul li.on
        {
            background-color: #e0e0e0;
        }

jQuery製作簡潔的多級聯動Select下拉框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.