简体   繁体   中英

Selecting a value from drop down changes the next drop down list

Hi Guys I am ruby on rails developer and have no idea about javascript or Jquery.

I have a select tag like this :

<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>

When a user selects for example CA - California PD from the above select tag, the next select tag which will be below it should have a drop down list like this :

 <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>

Thus a Javascript or JQuery function has to be used to collect the option selected in "form[city]" and to append F,L,U,S characters to the first two characters from "form[city]" select tag to the second select tag -> form[cityselected].

Thanks a lot in advance

Check this : 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);
}

I haven't tested this, but this is the general idea... it's VERY specific to your exact question.

Notice the addition of onSelect in the select html... that's the only html I've changed

<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)
   }

}

try this

$('#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 here

Below is the example : I am supposing you know the basics of 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>");
            }
            }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM