我修改了下面的脚本,发现第一个和第二个下拉菜单点击选项后无法关闭的问题。我希望它们的工作方式类似于第三个下拉菜单。
请帮助我。
我的代码:
<!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