简体   繁体   English

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

[英]Passing selected value of dropdownlist in URL

I have a dropdownlist that opens a new window when I select an item from the list. 我有一个下拉列表,当我从列表中选择一个项目时会打开一个新窗口。 How can I pass a dropdownlist value to the new window in a URL? 如何将下拉列表值传递到URL中的新窗口?

My code looks like this: 我的代码看起来像这样:

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

This is the JavaScript function: 这是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);
}

You can simply refer to this.value to get the value of the underlying select. 您可以简单地引用this.value来获取基础选择的值。

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

Also, this is easily handled in jQuery, if that's how you roll: 此外,这很容易在jQuery中处理,如果这是你如何滚动:

$(function(){

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

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

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

      return false;
  });
});

You can pass the dropdown object directly into the javascript like this... 您可以将下拉对象直接传递到这样的javascript中......

onchange="openWindow('add.aspx?ddlAddValue=', 800, 885, this)"

(Note, you do not need the javascript: prefix in an onchange command - the browser already knows it's javascript) (注意,在onchange命令中你不需要javascript:前缀 - 浏览器已经知道它是javascript)

And then in your function... 然后在你的功能......

function openWindow(url, windowHeight, windowWidth, dd)
{
   url = url + encodeURIComponent(dd.options[dd.selectedIndex].value);
   ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM