Click here to Skip to main content
15,925,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Everybody ,

I want to findout the list box selected values in asp.net using jquery can any body help?

its urgent?

Thanks in Advance
Posted

C#
$("#aSelect").click(function(){
    var selectedValues = [];
    $("#lstSelect :selected").each(function(){
        selectedValues.push($(this).val());
    });
    alert(selectedValues);
    return false;
});
 
Share this answer
 
$('#Select1').click(function(e) {
  $("#Select1").value());
});
 
Share this answer
 
Try this:
JavaScript
var sum = 0;
$('.ListBox1 option').each(function(index) {
  if ( ($(this).is(':selected')) {
    // do stuff if selected
    sum += this.value;
  }
  else {
   // this one isn't selected, do other stuff
  }
});

Also check HOW TO GET LISTBOX ID & SELECTED VALUE using jquery[^].


--Amit
 
Share this answer
 
v2
Comments
[no name] 15-Apr-13 1:51am    
i want to display all the selected values of the selected item in listbox

(i need to sum all the selected values and i need to store in a variable)
Member 10232382 11-Sep-13 5:18am    
how to check whether the listbox item is selected or not
_Amy 15-Apr-13 1:56am    
Take a variable and do the operation. See my modified codes..
check this

http://stackoverflow.com/questions/3297174/jquery-any-item-selected-in-listbox[^]

http://stackoverflow.com/questions/3113851/selecting-item-from-asp-net-listbox-using-jquery[^]


Here is the code
XML
<div>
      <asp:ListBox ID="ListBox1" runat="server" Width="50px" Height="100px"
          SelectionMode="Multiple">
          <asp:ListItem Value="One">1</asp:ListItem>
          <asp:ListItem Value="Two">2</asp:ListItem>
          <asp:ListItem Value="Third">3</asp:ListItem>
          <asp:ListItem Value="Four">4</asp:ListItem>
          <asp:ListItem Value="Five">5</asp:ListItem>
      </asp:ListBox>
      <br />
      <asp:TextBox ID="text1" runat="server"></asp:TextBox>
  </div>



XML
<script type="text/javascript">

    $("#<%=ListBox1.ClientID %>").change(function () {
        $("#<%=text1.ClientID %>").val($(this).val());
    });


</script>



Hope it helps...




Edited


Jquery code
C#
$("#<%=ListBox1.ClientID %>").change(function () {
      var selectedItems = $(this).val();
      var value = 0;
      for (var i = 0; i < selectedItems.length; i++) {
          value = parseInt(value)+ parseInt(selectedItems[i]);
      }
      $("#<%=text1.ClientID %>").val(value);
  });


Assuming the listbox is like this
XML
<asp:ListBox ID="ListBox1" runat="server" Width="50px" Height="100px"
         SelectionMode="Multiple">
         <asp:ListItem Value="1">1</asp:ListItem>
         <asp:ListItem Value="2">2</asp:ListItem>
         <asp:ListItem Value="3">3</asp:ListItem>
         <asp:ListItem Value="4">4</asp:ListItem>
         <asp:ListItem Value="5">5</asp:ListItem>
     </asp:ListBox>
 
Share this answer
 
v3
Comments
[no name] 15-Apr-13 1:51am    
i want to display all the selected values of the selected item in listbox

(i need to sum all the selected values and i need to store in a variable)
tuananh301191 22-Sep-13 0:01am    
Thanks, it works perfectly for me.

Best Regards
Naz_Firdouse 15-Apr-13 3:03am    
Here is the Jquery to sum all the selected values
$("#<%=ListBox1.ClientID %>").change(function () {
var selectedItems = $(this).val();
var value = 0;
for (var i = 0; i < selectedItems.length; i++) {
value = parseInt(value)+ parseInt(selectedItems[i]);
}
$("#<%=text1.ClientID %>").val(value);
});

Assuming your listbox items are like this

<asp:ListItem Value="1">1
<asp:ListItem Value="2">2
<asp:ListItem Value="3">3
<asp:ListItem Value="4">4
Hello
Check out this sample
http://jsfiddle.net/JthcU/[^]

You will get comma separated string of selected items.
If nothing is selected then it will return null.
 
Share this answer
 
Comments
[no name] 15-Apr-13 1:38am    
i want to display all the selected values of the selected item in listbox

(i need to sum all the selected values and i need to store in a variable)
vijay__p 15-Apr-13 1:53am    
http://jsfiddle.net/JthcU/1/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900