2011-09-26 50 views
3

我有下面的代碼,它不工作。我想將選定的文本放到我選擇的位置div中。有任何想法嗎?謝謝!把文字分隔與下拉選項

<select name='available_locations' class='select-location'> 
     <option value='Germany'>Germany</option> 
     <option value='Venice'>Venice</option> 
     <option value='Spain'>Spain</option> 
    </select> 

    <div id='selected-locations'> 

    </div> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<script type='text/javascript'> 
    $(document).ready(function() { 

     $('.select-location').change(function(){ 

         var location = $(this).attr('value'); 
         alert(location); 
         //new_text = $('#selected-locations').val() + location; 
         $('div.selected-locations').text(location); 


     }); 

    }); 

</script> 

回答

2

您需要在css selectorsjQuery selectors

$('div.selected-locations').text(location); 

閱讀起來應該是

$('div#selected-locations').text(location); 

OR

<div id='selected-locations'> 

</div> 

shoould是

<div class='selected-locations'> 

</div> 
+0

噢,那是行得通!我的錯。謝謝! – Emkey

0

你可以做到這一點

$('.select-location').change(function(){ 
    var location = $('.select-location option:selected').val(); 
    $('div#selected-locations').text(location); 
}); 

工作例如:http://jsfiddle.net/jasongennaro/F3A62/

需要與您的代碼來改變幾件事情:

  1. 變化$('div.selected-locations')$('div#selected-locations') ...期間哈希
  2. 使用option:selected
  3. 使用val()
0

使用
$( '#選擇-位置')文本(位置)。 你需要使用#作爲selected-locations是一個id而不是一個css類。