更改选择框中的选项

时间:2015-09-04 12:12:39

标签: jquery

两个选择框,第二个选择框选项应根据您在第一个选择的内容而改变。我怎么能用jquery做到这一点?

<select class="units">
    <option value="1">Sve ponude</option>
    <option value="2">Srbija</option>
    <option value="3">Grcka</option>
</select>
<select class="units">
    <option value="1">Sve ponude</option>
    <option value="2">Novi Sad</option>
    <option value="3">Beograd</option>
</select>

2 个答案:

答案 0 :(得分:1)

$(document).on('change', '.units', function () {
    var a = $('.units option:selected').val();

    alert(a);
    $('.units1').val(a);

});

<强> FIDDLE

UPDATE

<强> FIDDLE

答案 1 :(得分:1)

var one = ['Madison','Janesville', 'Green Bay'];
var two = ['Sacramento', 'Los Angeles', 'San Fransisco'];
var three = ['Richmond', 'Roanoke', 'Harrisonburg'];

$('#ddl1').change(function(){
    //clear values
    $('#ddl2').html('');

    //find which list to use
    var list = [];
    switch($(this).val()){
        case "1":
            list = one;
            break;
        case "2":
            list = two;
            break;
        case "3":
            list = three;
            break;
    }

    //populate dropdown
    $.each(list, function(index, value){
        $('#ddl2').append("<option>"+value+"</option>");
    });
});

https://jsfiddle.net/b1nygzxm/