2

I'm trying to show province/state field if country dropdown equals "Canada" or "United States" through Coffeescript.

So far I have (but looks over complex)

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

2 回答 2

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()
于 2013-02-22T04:14:22.603 回答
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()
于 2013-02-21T22:13:15.900 回答