2011-11-16 40 views

Trả lời

5

Something như thế này

var lastSelected = null; 
$('.multiSelectOptions').click(function(){ 
    lastSelected = this.value; 
}); 
+1

1. Không cần 'mỗi', và 2. không sử dụng' $ (this) .val() 'khi' this.value' này dễ dàng hơn. –

+0

@AndyE cảm ơn bạn về đầu vào, tốt hơn? :] –

+1

có, +1 cho bạn :-) –

0

Sử dụng this.value như trong câu trả lời ở trên không thành công khi người dùng đã Ctrl + nhấp và chọn nhiều mục - nó trả về giá trị của sự lựa chọn đầu tiên trong danh sách, thậm chí nếu điều đó không phải là lần nhấp cuối cùng. Hãy thử điều này:

var previouslySelected = []; 
$("#myMultiselect").change (function() { 
    // Get newly selected elements 
    var currentlySelected = $(this).val(); 
    var newSelections = currentlySelected.filter(function (element) { 
     return previouslySelected.indexOf(element) == -1; 
    }); 
    previouslySelected = currentlySelected; 

    if (newSelections.length) { 
     // If there are multiple new selections, we'll take the last in the list 
     var lastSelected = newSelections.reverse()[0]; 
    } 
}); 
Các vấn đề liên quan