2011-01-26 41 views
0

我已經使用了「select_tag」爲多選場:如何通過多選功能爲「select_tag」實現「Select/Unselect」?

%td 
    =select_tag('cars',options_from_collection_for_select(@cars, 'id', 'name'), {:multiple=>true,:size => 10}) 

它是好的,,用戶選擇的選項後,我想有以下特徵:「選擇之後,如果用戶點擊其他地方,則所有選擇的選項將返回到未選擇的「,這是多選字段返回到未選中狀態。

如何使用select_tag

回答

0

首先,這個問題與Rails或您使用select標記無關。 您可以用JavaScript灑在客戶端上處理這一點 - 這是一個使用jQuery的一個例子:

$('select').blur(function(){ 
    $('option', this).attr('selected', false); 
}); 

// This seems to work as well, haven't tested in anyhow further, though 
$('select').blur(function(){ 
    $(this).val(''); 
}); 

你可以看到它在這裏的行動:http://www.jsfiddle.net/jZNAn/1