2013-02-21 37 views
2

我試圖顯示省/州字段,如果國家下拉等於「加拿大」或「美國」通過Coffeescript。Coffeescript下拉值

到目前爲止,我有(但看起來反覆復)

canada = "Canada" 
usa = "United States" 
$('#order_shipping_address_attributes_country').change -> 
    selected = $('#order_shipping_address_attributes_country :value').text() 
    $('#shipping_province').show() if selected is canada 

回答

4

的以下作品:

$('#order_shipping_address_attributes_country').change -> 
    selected = $('#order_shipping_address_attributes_country option').filter(':selected').text() 
    if selected is "Canada" or selected is "United States" 
    $('#shipping_province').show() 
    else 
    $('#shipping_province').hide() 
4

你可以做

selected = $('#order_shipping_address_attributes_country option:selected').text() 

然而,根據http://api.jquery.com/selected-selector/,事情就會有更好的表現與

selected = $('#order_shipping_address_attributes_country option') 
      .filter(':selected').text() 
+0

感謝您指出按照jQuery推薦的方式。我最終使用了一些 – olimart 2013-02-22 04:16:11