从下拉列表中选择一个值会更改下一个下拉列表

时间:2013-01-22 07:36:30

标签: javascript jquery

嗨大家我是rails开发人员的ruby,不知道javascript或Jquery。

我有一个像这样的选择标签:

<select name="form[city]" id="form_city">
   <option value="">Select Office</option>
   <option value="WA - Washington PD">WA - Washington PD</option>
   <option value="CA - California PD">CA - California PD</option>
   <option value="NY - NewYork PD">NY - NewYork PD</option>
</select>

当用户从上面的选择标记中选择 CA - California PD 时,下面的下一个选择标记应该有一个下拉列表,如下所示:

 <select name="form[cityselected]" id="form_cityselected">
   <option value="CAF">CAF</option>
   <option value="CAL">CAL</option>
   <option value="CAU">CAU</option>
   <option value="CAS">CAS</option>
</select>

因此,必须使用Javascript或JQuery函数来收集在“form [city]”中选择的选项,并将F,L,U,S字符附加到“form [city]”选择标记中的前两个字符到第二个选择标签 - &gt;形成[cityselected]。

提前多多感谢

4 个答案:

答案 0 :(得分:3)

请检查:http://jsfiddle.net/Q8NW5/

$('#form_city').on('change', function (e) {
var val = $(':selected', $(this)).val(),
    val = val.split('-')[0],
    val = val.replace(/\s+/g, ''),
    char = ['F', 'L', 'U', 'S'],
    tpl = '<option val="{val}">{val}</option>',
    html = '';

$(char).each(function (item, value) {
    var opt = val + value;
    html += tpl.replace(/{val}/g, opt);
});

$('#form_cityselected').html(html).show();
$('#form_final_city').show();

var currCity = $('#form_cityselected option:selected').val();
setCity(currCity);

})

$('#form_cityselected').on('change', function () {
    var val = $(':selected', $(this)).val();
    setCity(val);
});

function setCity(city) {
    $('#form_final_city').val(city);
}

答案 1 :(得分:1)

我还没有对此进行过测试,但这是一般性的想法...它非常适合您的确切问题。

注意在select html中添加onSelect ...这是我改变的唯一html

<select name="form[city]" id="form_city" onSelect="updateOtherSelect(this)">
       <option value="">Select Office</option>
       <option value="WA - Washington PD">WA - Washington PD</option>
       <option value="CA - California PD">CA - California PD</option>
       <option value="NY - NewYork PD">NY - NewYork PD</option>
    </select>

function updateOtherSelect(select){
   //grab just what happens before the first space in your option's value
   var prefix = select.options[select.selectedIndex].value.split(" ")[0];

   //go through all your second drop down values and append, based on the string FLUS (if I understood your question correctly).
   for(i=0; i< document.getItemById("form_cityselected").options.length && i < "FLUS".length; i++){
       document.getItemById("form_cityselected").options[i].value = prefix + "FLUS".charAt(i)
   }

}

答案 2 :(得分:1)

试试这个

$('#form_city').change(function(){
    var cityselectedobj= $('#form_cityselected');
     cityselectedobj.empty()
    var val=$(this).val();
    var text=val.split(' ')[0];
    $('<option>').val(val + 'F').text(text+ 'F').appendTo(cityselectedobj);
    $('<option>').val(val + 'L').text(text+ 'L').appendTo(cityselectedobj);
    $('<option>').val(val + 'U').text(text+ 'U').appendTo(cityselectedobj);
    $('<option>').val(val + 'S').text(text+ 'S').appendTo(cityselectedobj);
)};

fiddle此处

答案 3 :(得分:1)

以下是示例: 我想你知道Jquery的基础知识。

$("#form_city").change(function() {
     // TODO 
filltheVillagecomboBox(villageStrings);//village strings seperatedBy '|'

 });


function filltheVillagecomboBox(villagesStrings){
            if(villagesStrings !=null){
                var villagesStrings = villagesStrings.split('|');

                box.append("<option value='null'>Please select your village</option>");

                for (var i = 0, l = villagesStrings.length-1; i <l; i++){
                    var id =villagesStrings[i].substring(0,2);
                    var village=villagesStrings[i].substring(0,villagesStrings[i].length)
                    $("#form_village").append("<option value="+village+">"+village+"</option>");
            }
            }

            }