1

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

请帮助我。

我的代码:

<!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

4

2 回答 2

1

有那个错误:

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
于 2013-09-05T17:47:31.633 回答
0

因为您试图修改 SelectBox 的 github 页面中给出的示例,所以很难遵循您的代码,变量名根本没有帮助。

无论如何,问题肯定出在您对 sb2 的重复调用中: sb2.sync()

可能,您需要再次复制 updateCities() 函数中的功能,因为您连续有三个框(而不是像示例中的两个框),以便正确更新和同步所有内容......

希望它对解决方案有所帮助...

于 2013-09-05T17:10:06.127 回答