2014-05-19 199 views
-3

我想用jQuery通过点击一个按钮来选择一个选项,比如说第3个项目,然后让DropDownList框改变它的值,比如'44'。使用jquery在按钮上点击选择下拉选项

  <asp:DropDownList ID="DropDownList2" runat="server" 
       onselectedindexchanged="DropDownList2_SelectedIndexChanged"> 
       <asp:ListItem Selected="True">SELECT</asp:ListItem> 
       <asp:ListItem Value="1">23</asp:ListItem> 
       <asp:ListItem Value="2">32</asp:ListItem> 
       <asp:ListItem Value="3">44</asp:ListItem> 
       <asp:ListItem Value="4">61</asp:ListItem> 
      </asp:DropDownList> 

回答

0

您可以使用:

$("#<%=DropDownList2.ClientID%>").val('3'); 
0

首先设置ClientIDModeStatic的ID可能会变化,而rednering。

<asp:DropDownList ID="DropDownList2" runat="server" ClientIDMode="Static" 
       onselectedindexchanged="DropDownList2_SelectedIndexChanged"> 
       <asp:ListItem Selected="True">SELECT</asp:ListItem> 
       <asp:ListItem Value="1">23</asp:ListItem> 
       <asp:ListItem Value="2">32</asp:ListItem> 
       <asp:ListItem Value="3">44</asp:ListItem> 
       <asp:ListItem Value="4">61</asp:ListItem> 
      </asp:DropDownList> 

    <input type="button" id="MyButton"/> 

然后你就可以像这样用jQuery代码:

$('#MyButton').click(function(){ 
    $('#DropDownList2').val("3"); 
}); 
0

尝试使用下面的代码:

使用值选择选项:

$('some_button_id').click(function() { 
    $('select[id*=DropDownList2]').val(3); //To select 44 
}); 

或者使用ASP.NET控件的ClientID:

$('some_button_id').click(function() { 
    $('select#<%=DropDownList2.ClientID%>').val(3); //To select 44 
}); 
0

这可以帮助你

var dropDownList2ID = "#<%=DropDownList2.ClientID%>"; 
var buttonID = "#idOfButton"; 

//... 

$(buttonID).click(function() { 
    $(dropDownList2ID).val(3); 
});