Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将选定的dropdownlist值附加到aspx页面本身的超链接查询字符串_Javascript_Asp.net_.net - Fatal编程技术网

Javascript 如何将选定的dropdownlist值附加到aspx页面本身的超链接查询字符串

Javascript 如何将选定的dropdownlist值附加到aspx页面本身的超链接查询字符串,javascript,asp.net,.net,Javascript,Asp.net,.net,我有一个dropdownlist。我想将它的选定值添加到超链接查询字符串中。如何使用block从aspx文件本身执行此操作?我的代码是这样的 <asp:DropDownList ID="ddlLanguage" runat="server" DataSourceID="ds" DataTextField="Langcode" DataValueField="LangKey" Width="200px"> </asp:DropDownList> <% if (ddl

我有一个dropdownlist。我想将它的选定值添加到超链接查询字符串中。如何使用block从aspx文件本身执行此操作?我的代码是这样的

<asp:DropDownList ID="ddlLanguage" runat="server" DataSourceID="ds" DataTextField="Langcode" DataValueField="LangKey" Width="200px"> </asp:DropDownList>

<% if (ddlLanguage.Items.Count>0) { %>
  <asp:HyperLink ID="hlnk" NavigateUrl="abcd.aspx?lkey=1" runat="server" />
<% } %>

0) { %>
如何将下拉列表的选定值传递给hlnk的NavigateUrl


请不要回答使用aspx.cs文件。我只希望它在sameline上完成。

要在不修改.cs文件的情况下动态更新
NavigateUrl
,您可以为下拉列表编写一个客户端
OnChange
事件,该事件只需更新输出
hlnk
控件的
href
属性在选定值前面加上前缀

这可以使用jQuery轻松实现

$('.language-dropdown').change(function()
{
    var newUrl = 'http://www.example.com/?id=' + $(this).val();
    $('.hyperlink').attr('href', newUrl)
});
或者,为了提高性能,您可以使用看起来像超链接但使用JavaScript的超链接重新定位到正确的url。这意味着url只更改一次,而不是每次更改

$('.hyperlink').click(function()
{
    var newUrl = 'http://www.example.com/?id=' + $('.language-dropdown').val();
    window.location(newUrl);
});

您面临的问题是,操作发生在客户端,因此当使用javascript发生下拉更改时,您需要调整链接的值(为了方便起见,我在这里使用了jQuery)

您可以实现一个基于jquery的函数来监视dd的更改,然后设置链接的url

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>
...
    <asp:DropDownList ID="ddlLanguage" runat="server" DataSourceID="ds" DataTextField="Langcode" DataValueField="LangKey" Width="200px"> </asp:DropDownList>

    <% if (ddlLanguage.Items.Count>0) { %>
     <a href="abcd.aspx?lkey=1" id="hlnk>Link to other location</a>
    <% } %>

    <script>
      var ddId = '<%=this.ddlLanguage.ClientID %>' 
       $('#'+ddId).change(function () {
           $('#hlnk').attr('href', 'abcd.aspx?lkey='+$(this).val());
       });
    </script>

...
</body>

...
变量ddId=''
$('#'+ddId).更改(函数(){
$('#hlnk').attr('href','abcd.aspx?lkey='+$(this.val());
});
...

伟大的理查兹思想相似:-)