javascript onchange事件到asp.net下拉列表

时间:2012-01-05 21:54:00

标签: javascript jquery drop-down-menu onchange

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

无服务器访问权限,无法访问代码隐藏:(

 <asp:DropDownList runat="server" id="DropDownList1" DataSourceID="spdatasource1" DataValueField="CategoryName" AutoPostBack="True" Onchange="window.open( Go to some link)">
                                                 </asp:DropDownList>

2 个答案:

答案 0 :(得分:7)

var selectedOption = $("#DropDownList1 option:selected").text();

$("#DropDownList1").change(function(e) {
      url: "mysite.com/default.aspx?key=" + selectedOption;
      window.location = url;
});

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

http://tinyurl.com/82ter35

的另一种选择(可能更好)
$(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!");
      }
    });
  });
});

答案 1 :(得分:1)

我不确定你想做什么,但我猜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