2016-02-28 107 views
0

我有2個下拉列表。 一個下拉列表是國家。另一個下拉是貨幣。 我想用jQuery來控制用戶選擇國家的貨幣基礎。 因此,無論何時用戶更改國家,貨幣更改。根據某個下拉列表更新下拉列表的值

我的第一步是嘗試獲取選定的值(國家名稱),當用戶選擇它。

//Auto choose currency 
    country = jQuery('#input_1_15 :selected').text(); 
    jQuery("#input_1_15").change(function() { 
     country = jQuery('#input_1_15 :selected').text(); 
    }); 
    alert(country); 

但是,當用戶選擇時不打印出國名。
https://jsfiddle.net/uqvgsk6j/

+0

而問題是什麼? –

+0

對不起,我更新了我的問題。 – John

+0

將警報移至「更改」處理程序 –

回答

1

如果你有相同的順序使用

$(function() { 
    $("#input_1_15").on("change",function() { 
     $("#input_1_16").get(0).selectedIndex=$(this).get(0).selectedIndex; 
    }); 
}); 
+0

謝謝。我有相同的順序。但法國,德國,意大利使用相同的貨幣。你的代碼能工作嗎? – John

+0

只有你有3歐元的選擇。 – mplungjan

0

https://jsfiddle.net/refan/LtcjzoLp/3/

,如果你可以添加數據屬性選項:

<option data-cur="JPY" value="Nhật Bản">Nhật Bản</option> 

jQuery("#input_1_15").change(function() { 

    var selectedCurrency = $(this).find(":selected").data("cur"); 
    $("#input_1_16").val(selectedCurrency); 

}); 
0

jQuery的:

$(function(){ 
    $("#input_1_15").on("change", function() {  
     document.getElementById("input_1_16").selectedIndex = document.getElementById("input_1_15").selectedIndex; 
    }); 
}); 

your fiddle updated

相關問題