Use Jquery To Select A Dropdown Option On Button Click
I want to use jQuery to select an option, say the 3th item, in a DropDownList by clicking a button, then have the DropDownList box change it's value, say '44'.
Solution 1:
You can use:
$("#<%=DropDownList2.ClientID%>").val('3');
Solution 2:
First set ClientIDMode
to Static
as ID may be change while rednering.
<asp:DropDownListID="DropDownList2"runat="server"ClientIDMode="Static"onselectedindexchanged="DropDownList2_SelectedIndexChanged"><asp:ListItemSelected="True">SELECT</asp:ListItem><asp:ListItemValue="1">23</asp:ListItem><asp:ListItemValue="2">32</asp:ListItem><asp:ListItemValue="3">44</asp:ListItem><asp:ListItemValue="4">61</asp:ListItem></asp:DropDownList><inputtype="button"id="MyButton"/>
Then you can do like this with Jquery Code:
$('#MyButton').click(function(){
$('#DropDownList2').val("3");
});
Solution 3:
Try using the following code:
Using the value to select the option:
$('some_button_id').click(function() {
$('select[id*=DropDownList2]').val( 3 ); //To select 44
});
Or using the ClientID of the ASP.NET control:
$('some_button_id').click(function() {
$('select#<%=DropDownList2.ClientID%>').val( 3 ); //To select 44
});
Solution 4:
This might help you
var dropDownList2ID = "#<%=DropDownList2.ClientID%>";
var buttonID = "#idOfButton";
//...
$(buttonID).click(function() {
$(dropDownList2ID).val(3);
});
Post a Comment for "Use Jquery To Select A Dropdown Option On Button Click"