2012-02-02 77 views
2

我有一個多重選擇下拉框。每次選擇某個項目時,我都需要進行ajax調用,但在從選擇菜單中獲取特定值時遇到問題。不是檢索當前選定的值,而是獲取所有選定值的數組。如何使用JQuery獲取當前選定的值?如何使用Jquery獲取多重選擇的當前選定值

+1

有你有機會來看看文檔在http://api.jquery.com/選擇的選擇器/ – kush 2012-02-02 18:52:25

回答

1

OK,就想通了:

$(this).val(); 

以前我是用$('#id').val(),那就是返回數組。 $(this).val()會爲您提供當前選定的值。

0

嘗試使用點擊事件,根據this answer來捕獲它,因爲每個項目被選中。

0

您可以在警報得到選擇的值:

$("#mydropdown").on('change', function(evt, params) { 
alert(params.selected); 
}); 

這將幫助你得到你想要的精確值。

0

這裏是小提琴從多選下拉列表中獲得當前所選的值

var selectedOption; 
 
$(document).ready(function() { 
 
    $('#price-type').on('change', function(evt, params) { 
 
    var currentSelection; 
 
    if (selectedOption) { 
 
     var currentValues = $(this).val(); 
 
     currentSelection = currentValues.filter(function(el) { 
 
     return selectedOption.indexOf(el) < 0; 
 
     }); 
 
    } 
 
    selectedOption = $(this).val(); 
 
    $('#result').text(currentSelection); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id="price-type" multiple="multiple"> 
 
    <option value="PRICE-1">PRICE-1</option> 
 
    <option value="PRICE-2">PRICE-2</option> 
 
    <option value="PRICE-3">PRICE-3</option> 
 
    <option value="PRICE-4">PRICE-4</option> 
 
    <option value="PRICE-5">PRICE-5</option> 
 
</select> 
 

 
<div id="result"> 
 

 
</div>

相關問題