[英]Selecting a value from drop down changes the next drop down list
嗨,大家好我是Rails開發人員的紅寶石,對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]”選擇標記的前兩個字符中選擇標簽->表單[cityselected]。
提前謝謝
檢查一下: 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);
}
我還沒有測試過,但這是一般性的想法……這完全取決於您的確切問題。
請注意,在選擇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)
}
}
嘗試這個
$('#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);
)};
在這里擺弄
下面是示例:我假設您了解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>");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.