如何动态更改下拉值

时间:2023-01-07 13:14:46

I need you help as I am not good in jQuery. I have bellow code and want to do some changes.

我需要你的帮助,因为我不擅长jQuery。我有下面的代码,并希望做一些更改。

If user Select One/Two(drop-down one) and HDB(drop-down second) then i want change this Boat Quay, Chinatown To Admiralty, Alexandra Road in third drop-down. One more thing that i want to set One(drop-down one) by default every time.

如果用户选择一个/两个(下拉一个)和HDB(下拉秒)那么我想要改变这个驳船码头,唐人街到金钟,亚历山德拉路的第三个下拉。还有一件事我想每次都默认设置One(下拉列表)。

My code reference link: roblaplaca

我的代码参考链接:roblaplaca

I will thankful If anyone can help me :)

我会感激如果有人可以帮助我:)

    <!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>
<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="selectone">Select a Type</option>
          <option value="rfs">One</option>
          <option value="rfr">Two</option>
          <option value="cfs">Three</option>
          <option value="cfr">Four</option>
        </select>

        <select class="custom interactive" id="TypeList">
          <option>One</option>
          <option>Two</option>
          <option>Three</option>
          <option>Four</option>
        </select> 

        <select class="custom">
          <option value="">Any</option>
          <option value="">Boat Quay</option>
          <option value="">Chinatown</option>
        </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
    */
    $("select.custom").not(".interactive").each(function() {
        var sb = new SelectBox({
            selectbox: $(this),
            height: 150,
            width: 200
        });
    });

    /*
        Adding some extra functionality for "interactive" selects
    */
    var TypeList = {
        selectone: ["Select a Type"],
        rfs: ["Any", "Landed", "Condominium", "HDB", "Others"],
        rfr: ["Any", "Landed", "Condominium", "HDB", "Others"],
        cfs: ["Any", "Industrial", "Retail", "Land", "Office", "Others"],
        cfr: ["Any", "Industrial", "Retail", "Land", "Office", "Others"]
        }

    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();
                    }
                }
            }));
        } else if ($(this).attr("id") === "TypeList") {
            city_SB = new SelectBox($.extend(defaultSelectboxSettings, {
                selectbox: $(this)
                }));
        }
    });

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

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

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

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

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

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

1 个解决方案

#1


2  

here's a code snippet you can use to add options:

这是您可以用来添加选项的代码段:

var newOption = document.createElement("option");
newOption.value="newValue";
newOption.innerHTML = "Option Text";

$("#properties").append(newOption);

to remove an option use

删除选项使用

$("#idOfTheOption").remove();

just repeat that with all objects you want to change.

只需对要更改的所有对象重复此操作即可。

since you have arrays, you could do

因为你有阵列,你可以做到

$("#properties").html("");

for(var i=0; i<array.length;i++ ){
   var newOption = document.createElement("option");
   newOption.value=array[i];
   newOption.innerHTML = array[i];

   $("#properties").append(newOption);
}

#1


2  

here's a code snippet you can use to add options:

这是您可以用来添加选项的代码段:

var newOption = document.createElement("option");
newOption.value="newValue";
newOption.innerHTML = "Option Text";

$("#properties").append(newOption);

to remove an option use

删除选项使用

$("#idOfTheOption").remove();

just repeat that with all objects you want to change.

只需对要更改的所有对象重复此操作即可。

since you have arrays, you could do

因为你有阵列,你可以做到

$("#properties").html("");

for(var i=0; i<array.length;i++ ){
   var newOption = document.createElement("option");
   newOption.value=array[i];
   newOption.innerHTML = array[i];

   $("#properties").append(newOption);
}