javascript onchange事件到asp.net的dropdownlist

javascript onchange event to asp.net dropdownlist

本文关键字:net dropdownlist asp onchange 事件 javascript      更新时间:2023-09-26

是否可以使用javascript将下拉列表选择触发发布回同一页面,并将所选内容作为querystring添加到url?问题是该列表是从某个共享点列表动态加载的。如果我的网站是mysite.com/default.aspx因此,当做出选择时,它应该重定向到mysite.com/default.aspx?key=选择

无法访问服务器,无法访问代码后台:(

 <asp:DropDownList runat="server" id="DropDownList1" DataSourceID="spdatasource1" DataValueField="CategoryName" AutoPostBack="True" Onchange="window.open( Go to some link)">
                                                 </asp:DropDownList>
var selectedOption = $("#DropDownList1 option:selected").text();
$("#DropDownList1").change(function(e) {
      url: "mysite.com/default.aspx?key=" + selectedOption;
      window.location = url;
});

未测试,也不确定重新加载页面的事件,例如(提交或锚定)

另一种选择可能是:

$(document).ready(function() {
  
   var selectedOption = $("#DropDownList1 option:selected").text();
  $("#DropDownList1").change(function() {
    $.ajax({
      type: "POST",
      url: "mysite.com/default.aspx?key=" + selectedOption,
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        alert("this worked!");
      }
    });
  });
});

我不确定你想做什么,但我想jquery是你的问题的正确答案

无论如何,这可能会有所帮助:

<script language="javascript" type="text/javascript">
    function xx(e) {
        alert("fired by " + "<%= DropDownList1.UniqueID %>" + "change ");
        __doPostBack("<%= DropDownList1.UniqueID %>", "");
    }
</script>

<asp:DropDownList ID="DropDownList1" runat="server" onchange="xx()">
    <asp:ListItem>q</asp:ListItem>
    <asp:ListItem>w</asp:ListItem>
    <asp:ListItem>e</asp:ListItem>
    <asp:ListItem></asp:ListItem>
</asp:DropDownList>

代码隐藏(VB.NET)

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    MsgBox("Do whatever you want here")
End Sub