【问题标题】:use jquery to select a dropdown option on button click control使用 jquery 在按钮单击控件上选择下拉选项
【发布时间】:2014-05-19 19:06:25
【问题描述】:

我已经尝试过这段代码。

<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 的任何替代方法 谢谢

【问题讨论】:

  • 您的大写按钮似乎坏了...
  • OP 提出的重复问题。 @user3651976 您应该让回答者回答您的其他问题中没有解决的问题
  • 请准确地提出您的问题。

标签: jquery html asp.net


【解决方案1】:

服务器端获得不同的 ID:

改为这样做:

$('#&lt;%= MyButton.ClientID%&gt;').click(function () {...

也把它移到这里:(你不能注册未来的元素(除非使用.on)或document.ready

【讨论】:

  • OP 的 ClientIDMode="Static" 导致 ClientID 与服务器端 ID 相同
  • @Adil 不在按钮上!这就是为什么它不起作用这就是为什么我不明白它是如何重复的。
猜你喜欢
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 2013-01-14
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多