2011-07-21 48 views
1

,如果我有bookd_ids和book_titles在數據庫如下:如何獲得多個選擇IDS

id | title 
--------------- 
1 | abc 
2 | xyz 
3 | pqr 

的觀點如下所示::

<table> 
<tr> 
    <td> 
    <%= select("books", "book_id", Book.all.collect {|b| 
      [ b.title, b.id ] },{:include_blank => false})%> 
    </td> 
    <td> 
    <img src="/images/add_icon.png" 
     alt="Add Book" width="20px" height="20px" onclick="AddBook()" /> 
    </td> 
</tr> 
<tr> 
    <td id="current"> 
    <br /> 
    Current Books<br /> 
    <p id="none" style="font-style: italic; font-weight: lighter"> 
    None Selected</p> 
    </td> 
</tr> 
<tr> 
    <td id="tdbook"></td> 
    <td id="tdbookimg"></td> 
</tr> 
</table> 

當我點擊add_icon.png圖像,它會將選定的圖書添加到選定的圖書標題下。如果我多次這樣做,它會在選定的書籍下添加多個選定的行。

的AddBook()函數寫爲:

function AddBook(){ 
count = count + 1; 
$('#none').hide(); 

var id = $("#books_book_id").val(); 

$("#tdbook").append("<p id='row" + id + "'><label for='txt" + id + "'>" +$("#books_book_id option:selected").text() + "</label></p>"); 
$("#tdbookimg").append("<p id='rowimg" + id + "'></p>"); 
} 

我能得到一本通的jQuery這樣的ID:

VAR ID = $( 「#books_book_id」)VAL( );

如何在添加多個項目時獲取多個ID。這樣我就可以獲得控制器中的值。

感謝, 庫馬爾

回答

1

Kumar--嘗試給選定的書籍類,如:

$(".selected").val(); 
:這樣以後

$("#books_book_id").addClass("selected"); 

你可以選擇所有