使用javascript填充下一个选择选项

时间:2014-02-23 12:19:20

标签: javascript

所以我有3个选择选项。品牌模型和部分。当用户点击品牌时,下一个选择选项应该为该品牌提供模型。例如:

Kawasaki:
     -Curve
     -Boxer 150
     -CT100
     -Fury
Honda:
    -BeAT 110
    -Wave
    -CB110
    -TMX Supremo

Suzuki:
    -Nex 115
    -Thunder 125
    -Hayate 125 
    -Skydrive 125

Yamaha:
    -Mio 125
    -Mio Soul
    -Fino
    -Vega

我找到了一个可以填充下一个选择选项的脚本,我试图编辑它以将模型放在里面,但它不在这里是脚本:

<script type="text/javascript">
$(function(){
    $("#Model").attr("disabled","true");
    $("#Brand").change(function(){
        if(this.value != ""){
          $('#Model').removeAttr('disabled');
           var brand = $("#Brand option:selected").text();
            document.getElementById("Model").options.length = 1;
            for(var i = 0; i < 10 ;i++){
                var selectOption = document.createElement("OPTION");
                selectOption.text = brand + " Model No." + i;
                document.getElementById('Model').appendChild(selectOption);
            } 
$("#Model").selectmenu("refresh","true");            
        }
        else{
            $("#Model").attr("disabled","true"); 
            $("#Model").val("");            
            $("#Model").selectmenu("refresh","true");      
        }
    });
})
</script>

这是我的表格:

<table>
        <form style="position:absolute;left: 15%;top: 25%;" action="orderfilterbrand.php" method="post">
    <div class="stepsClass"><div id="mainselection">
    <h5>Choose Your Motorcycle Brand: </h5>

                <select id="Brand" name="Brand">
            <option value = "">Motorcycle Brand</option>
            <option value = "Kawasaki">Kawasaki</option>
            <option value = "Honda">Honda</option>
            <option value = "Suzuki">Suzuki</option>
            <option value = "Yamaha">Yamaha</option>
            </select>
                </div></div>
                  <div class="stepsClass"><div id="mainselection">
                    <h5>Choose Your Motorcycle Model: </h5>
            <select id="Model" name="Model">
                <option>Motorcycle Model</option>
            </select>
                      </div></div>
                    <div class="stepsClass">
                    <div id="mainselection">
                            <h5>Choose Your Motorcycle Exterior Part: </h5>
            <select id="Part" name="Part">
            <option>Select Motorcycle Part</option>
            <option>Muffler</option>
            <option>Side Mirror</option>
            <option>Windshield</option>
            <option>Rim</option>
            <option>Seat</option>
            <option>Handle Bar</option>
            </select>
            <td><button class="btn btn-primary btn-lg"  name="" type="submit">Search Part</button></td>
    </div></div>
</form>
        </table>

如何将模型放入脚本中? Thnx提前先生和女士:)

1 个答案:

答案 0 :(得分:1)

创建一个可以使用值访问的地图,例如

var map = {
    Kawasaki: ['-Curve', '-Boxer 150', '-CT100', '-Fury'],
    Honda   : ['-BeAT 110', '-Wave', '-CB110', '-TMX Supremo'],
    Suzuki  : ['-Nex 115', '-Thunder 125', '-Hayate 125', '-Skydrive 125'],
    Yamaha  : ['-Mio 125', '-Mio Soul', '-Fino', '-Vega']
};

然后你可以像这样使用它

$(function () {
  $("#Model").prop("disabled", true);
  $("#Brand").change(function () {
      if (this.value in map) {
        $('#Model').prop('disabled', false).empty();
        var brands = map[this.value];

        $.each(brands, function() {
            $('#Model').append(
                $('<option />', {value : this, text : this})
            );
        });

        $("#Model").selectmenu("refresh", true);
    } else {
        $("#Model").prop("disabled", true).val("").selectmenu("refresh", true);
    }
  });
});

FIDDLE