javascript - javascript 和 html 出现问题

标签 javascript jquery drop-down-menu

我修改了下面的脚本,发现点击选项后第一个和第二个下拉菜单无法关闭的问题。我希望它们的工作方式与第三个下拉菜单类似。

请帮助我。

我的代码:

<!doctype html>
<html>
<head>
<title>Custom Styled Selectbox</title>
<link rel="stylesheet" href="http://www.roblaplaca.com/docs/custom-selectbox/css/customSelectBox.css" />
</head>
<body class="noJS">
<script type="text/javascript"> 
try{Typekit.load();}catch(e){}
    var bodyTag = document.getElementsByTagName("body")[0];
    bodyTag.className = bodyTag.className.replace("noJS", "hasJS");
</script>
<style type="text/css">
.hasJS select.custom1 {
    position: absolute;
    left: -999em;
}
</style>
<div class="grid-system clearfix">
    <div class="row">
        <div class="col span-9">
            <div class="example clearfix">
                <select class="custom interactive" id="properties">
                    <option value="Residential for Sale" selected>Residential for Sale</option>
                    <option value="Residential for Rent">Residential for Rent</option>
                    <option value="Commercial for Sale">Commercial for Sale</option>
                    <option value="Commercial for Rent">Commercial for Rent</option>
                </select>

                <select class="custom interactive" id="TypeList">
                  <option selected>Residential for Sale</option>
                  <option>Residential for Rent</option>
                  <option>Commercial for Sale</option>
                  <option>Commercial for Rent</option>
                </select>

                <select class="custom1 interactive1" id="TypeList1">

                </select>
             </div>
        </div>
    </div>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> 
<script src="http://github.com/vitch/jScrollPane/raw/master/script/jquery.jscrollpane.js"></script> 
<script src="http://www.roblaplaca.com/docs/custom-selectbox/js/SelectBox.js"></script> 
<script type="text/javascript"> 
$(function() {
    window.prettyPrint && prettyPrint()
    /*
        This is how initialization normally looks. 
        Typically it's just $("select.custom"), but to make this example more clear 
        I'm breaking from the pattern and excluding interactive
    */
    var sb, sb2;
    $("select.custom").not(".interactive").each(function() {
        sb = new SelectBox({
            selectbox: $(this),
            height: 150,
            width: 200
        });
    });

    $("select.custom1").not(".interactive").each(function() {
        sb2 = new SelectBox({
            selectbox: $(this),
            height: 150,
            width: 250
        });
    });

    /*
        Adding some extra functionality for "interactive" selects
    */
    var TypeList = {
        //selectone: ["Any"],
        'Residential for Sale': ["Any", "Landed", "Condominium", "HDB", "Others"],
        'Residential for Rent': ["Any", "Landed", "Condominium", "HDB", "Others"],
        'Commercial for Sale': ["Any", "Industrial", "Retail", "Land", "Office", "Others"],
        'Commercial for Rent': ["Any", "Industrial", "Retail", "Land", "Office", "Others"]
        }

    var TypeListVal = {
        //selectone: ["Any"],
        'Residential for Sale': ["Any", "1", "2", "3", "4"],
        'Residential for Rent': ["Any", "1", "2", "3", "4"],
        'Commercial for Sale': ["Any", "5", "6", "7", "8", "9"],
        'Commercial for Rent': ["Any", "5", "6", "7", "8", "9"]
        }


    var defaultSelectboxSettings = {
        height: 150,
        width: 150
    };

    var country_SB = null,
    city_SB = null;

    $("select.interactive").each(function() {
        if ($(this).attr("id") == "properties") {
            country_SB = new SelectBox($.extend(defaultSelectboxSettings, {
                selectbox: $(this),
                changeCallback: function(val) {
                    if (TypeList[val]) {
                        city_SB.enable();
                        updateCities(val);
                    }
                    if (val == "selectone") {
                        city_SB.disable();
                    }

                    <!------------------------------Location---------------------------->
                    var getType = jQuery( "#TypeList option:selected" ).text();
                    if(getType == "HDB"){

                        var select = document.getElementById("TypeList1");
                        select.options.length = 0;

                        var myobject = [
                            ['Any', 'Any'],
                            ['1', 'Boat Quay'],
                            ['2', 'Chinatown'],
                            ['3', 'Havelock Road'],
                            ['4', 'Marina Square'],
                            ['5', 'Raffles Place']
                        ];

                        var select = document.getElementById("TypeList1");
                        for (var i=0, len = myobject.length; i<len; i++){
                            select.options[select.options.length] = new Option(myobject[i][1], myobject[i][0]);
                        }
                        sb2.sync();

                    }else{

                        var select = document.getElementById("TypeList1");
                        select.options.length = 0;

                        var myobject = [
                            ['Any', 'Any'],
                            ['1', 'D01 Boat Quay / Raffles Place'],
                            ['2', 'D02 Chinatown / Tanjong Pagar'],
                            ['36', 'D03 Alexandra / Commonwealth'],
                            ['37', 'D04 Harbourfront / Telok Blangah'],
                            ['38', 'D04 Harbourfront / Telok Blangah']
                        ];

                        var select = document.getElementById("TypeList1");
                        for (var i=0, len = myobject.length; i<len; i++){
                            select.options[select.options.length] = new Option(myobject[i][1], myobject[i][0]);
                        }
                        sb2.sync();

                    }
                    <!------------------------------Location---------------------------->

                }
            }));
        } else if ($(this).attr("id") === "TypeList") {
            city_SB = new SelectBox($.extend(defaultSelectboxSettings, {
                selectbox: $(this)
                }));
        }

    });

    updateCities($("#properties").val());

    if ($("#properties").val() == "selectone") {
       //city_SB.disable();
    }

    <!-------------------------------------------------------------Location-------------------------------------------------------------------------->
    if(jQuery( "#TypeList option:selected" ).text()){

        var myobject = [
            ['Any', 'Any'],
            ['1', 'D01 Boat Quay / Raffles Place'],
            ['2', 'D02 Chinatown / Tanjong Pagar'],
            ['36', 'D03 Alexandra / Commonwealth'],
            ['37', 'D04 Harbourfront / Telok Blangah'],
            ['38', 'D04 Harbourfront / Telok Blangah']
        ];

        var select = document.getElementById("TypeList1");
        for (var i=0, len = myobject.length; i<len; i++){
            select.options[select.options.length] = new Option(myobject[i][1], myobject[i][0]);
        }
        sb2.sync();
    }
    <!-------------------------------------------------------------Location--------------------------------------------------------------------------->

    function updateCities(val) {
        var $select = $("select#TypeList"),
        html = "";

        for (var i = 0; i < TypeList[val].length; i++) {
            html += '<option value="'+ TypeListVal[val][i] +'">' + TypeList[val][i] + '</option>';
        }
        $select.html(html);

        // HACK: chrome is too fast?
        setTimeout(function() {
            city_SB.sync();
        }, 1);
    }

});         
</script>
</body>
</html>

预先感谢您的帮助。 :)

示例 jsbin

http://jsbin.com/EjaQIzus/2/edit

最佳答案

有这个错误:

Timestamp: 9/5/2013 1:45:28 PM
Error: TypeError: $dl.jScrollPaneRemove is not a function
Source File: http://www.roblaplaca.com/docs/custom-selectbox/js/SelectBox.js
Line: 238

关于javascript - javascript 和 html 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640591/

相关文章:

javascript - 具有垫表初始化的 Angular Material 2分页器

javascript - 开放时间通知(07 :30 tot 22:00) Javascript

javascript在下拉值更改时将属性设置为只读

ios - 以编程方式创建可滚动下拉菜单(iOS/Swift)

javascript - 在 IE8+ 中旋转可点击元素

javascript - 根据 AngularJS 中模型的值更改类

jquery - 使用 jquery 改变背景颜色

javascript - 如何使用jquery将html对象连接到html字符串?

javascript - DIV 中的文本对齐(TEXT 没有空格)

html - 如何制作纯 CSS bootstrap onclick 和 hover 下拉菜单?