2012-07-19 2 views
2

ASP.NET ListBox가 있으며 변경시 (단일 또는 다중) 선택된 텍스트를 (단일 또는 다중) 경고로 표시해야합니다. 경고 수는 선택한 항목 수와 같아야합니다. 나는 다음과 같은 코드를 시도했다. ListBox의 첫 번째 항목을 보여주는 추가 경고가 발생한다. 나는 어디로 잘못 갔는가?jQuery ListBox 하나 또는 여러 개의 선택된 항목

<asp:ListBox ID="ListBox1" runat="server" Width="100px" 
    SelectionMode="Multiple"> 
    <asp:ListItem Selected="True" Value="1">White</asp:ListItem> 
    <asp:ListItem Selected="False" Value="2"> Silver </asp:ListItem> 
    <asp:ListItem Value="3"> Dark Gray </asp:ListItem> 
    <asp:ListItem Value="4"> Khaki </asp:ListItem> 
    <asp:ListItem Value="5"> Dark Khaki </asp:ListItem> 
</asp:ListBox> 

$(document).ready(function() { 
    $("#ListBox1 ").change(function() { 
     $("option").each(function() { 
      if (this.selected) { 
       alert(this.text); 
      } 
     }); 
    }); 
}); 

도와주세요.

감사합니다.

... 
<option value="1" selected="true"> 
<option value="2" selected="false"> 
... 

"선택"속성이 두 경우 모두에 존재하기 때문에

, this.selected 확인하는 경우는 true 반환합니다

+0

, http://jsfiddle.net/9zthu/2/이와

$("#ListBox1 ").change(function() { $("option").each(function() { if (this.selected) { alert(this.text); } }); }); 

:

같은 목적을 달성하기 쉬운 방법이를 대체하는 것입니다 – ocanal

답변

1

나는 어떤 일이 일어나고 것은 당신의 ASP 코드는 HTML이 같이 많은 ListItems를 렌더링하는 것을 생각 "selected"가 존재하기 때문에 속성은 true 또는 false로 설정됩니다. 그것은 잘 작동 보인다

$('#ListBox1').find('option:selected').map(function() { 
    alert.log($(this).text()); 
});