使用jquery在按钮单击控件上选择下拉选项

时间:2014-05-19 10:57:01

标签: jquery html asp.net

我试过这段代码。

<title></title>
<script type="text/javascript" src="JavaScripts/jquery-1.4.1.js"> </script>
<script type="text/javascript">
  $('#MyButton').click(function () {
    $('#DropDownList2').val("3");;
  });
</script>



<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>

<asp:Button ID="MyButton" runat="server" Text="MAX" />

但是工作不是缝合。我不知道我是否使用了正确版本的jquery文件。 请检查 如果有或没有使用jquery有任何替代方案 谢谢

1 个答案:

答案 0 :(得分:1)

服务器端获得不同的 ID:

请改为:

$('#<%= MyButton.ClientID%>').click(function () {...

同样将其移至此处:(您无法注册未来元素(除非使用.on)或document.ready

enter image description here