2010-03-25 75 views
1

我有如下選擇值:讀取選項值中選擇

<select id="SelectBox" multiple="multiple"> 

      <% foreach (var item in Model.Name) 
      { %> 
       <option value="<%= item.Value %>"><%=item.Text%></option> 
      <% } 
      %> 

      </select> 

我有jQuery的一個功能,將要讀文本和值。我需要在數組中具有值,以便我可以在具有列ID和另一列文本的表中顯示它們。我的問題是我有一行文本test1test2test3。

function read() { 
      $("#SelectBox").each(function() { 
       var value = $(this).val(); 
       var text = $(this).text(); 
       alert(value);alert(text); 

      }); 
     } 

回答

2

你很近。您需要遍歷<option> S,不是<select>元素:

$("#SelectBox option").each(function() { 
    var value = $(this).val(); 
    var text = $(this).text(); 
    alert(value); 
    alert(text); 
} 
1

嘗試

function read() { 
      $("#SelectBox > option").each(function() { 
       var value = $(this).val(); 
       var text = $(this).text(); 
       alert(value);alert(text); 

      }); 
     }