javascript - 在 URL 中传递下拉列表的选定值

标签 javascript asp.net drop-down-menu

我有一个下拉列表,当我从列表中选择一个项目时,它会打开一个新窗口。如何将下拉列表值传递到 URL 中的新窗口?

我的代码是这样的:

<asp:DropDownList ID="ddlAdd" runat="server" CssClass="ddl" OnChange="javascript:openWindow('add.aspx?ddlAddValue=', 800, 885)">
    <asp:ListItem>One</asp:ListItem>
    <asp:ListItem>Two</asp:ListItem>
    <asp:ListItem>Three</asp:ListItem>
</asp:DropDownList>

这是 JavaScript 函数:

function openWindow(url, windowHeight, windowWidth)
{
    var centerHeight = (screen.height - windowHeight) / 2;
    var centerWidth = (screen.width - windowWidth) / 2;
    var features = "height=" + windowHeight + ", width=" + windowWidth + ", top=" + centerHeight + ", left=" + centerWidth + ", scrollbars=" + 1;
    var popUp = window.open(url, "", features);
}

最佳答案

您可以简单地引用this.value 来获取底层select 的值。

OnChange="javascript:openWindow('add.aspx?ddlAddValue=' + escape(this.value), 800, 885)"

此外,这在 jQuery 中很容易处理,如果您是这样的话:

$(function(){

  $('select[id$=ddlAdd]').change(function () {

      var addValue = $(this).val();

      if (addValue) { 
          // window opening logic here
      }

      return false;
  });
});

关于javascript - 在 URL 中传递下拉列表的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11800746/

相关文章:

javascript - 正则表达式从 URL 获取特定参数

javascript - 等待与 Mocha 未兑现的 promise

javascript - 注入(inject)错误

asp.net - 从 ASP.NET 应用程序 (IIS 7,5) 中运行控制台应用程序 (.exe)

javascript - 具有数据属性的多个下拉菜单切换

javascript - 根据多个下拉菜单的结果加载特定div

javascript - 对象如何在 JavaScript 中工作

asp.net - 无论行已加载到 EF 更改跟踪器中还是未加载到 Entity Framework 中,如何更新实体?

c# - c#的消息系统

javascript - 边缘浏览器: mouseleave event triggered when changing select value