简体   繁体   English

如何在单击锚点后更改锚点的href?

[英]How can I change the href of an anchor after it has been clicked?

I have a page that pulls in data via AJAX. 我有一个通过AJAX提取数据的页面。 As well as this I have a link to download said data as a CSV file. 除此之外,我还有一个链接可以将所述数据下载为CSV文件。

The problem I have is that upon I need to pass some parameters along with the click event so that the server can return the correct CSV file. 我遇到的问题是,我需要传递一些参数以及click事件,以便服务器可以返回正确的CSV文件。

Without any processing the link looks like this: 没有任何处理,链接看起来像这样:

<a href="/manager/TargetResults.csv" class="black">Download Target Reports</a>

This then fires off an ASP.NET MVC Controller action that returns the CSV. 然后,这将触发返回CSV的ASP.NET MVC Controller操作。 What I want to do though is pass two parameters along in the query string. 我想要做的是在查询字符串中传递两个参数。 Initially I thought that I could intercept the click event and add data to the query string like so: 最初我认为我可以拦截click事件并将数据添加到查询字符串,如下所示:

var holder = $('.hidden-information').first();                                   
var newOutlets = $('input[name="newoutlets"]', holder).val();                                   
var queryDate = $('input[name="enddate"]', holder).val();                                    
var anchor = $(this);                                    
var link = anchor.attr('href');                                   
link = link + "?endDate=" + queryDate + "&newOutlets=" + newOutlets;
anchor.attr('href', link);

It would seem that changing the href at this stage will not update the link in time and the URL will be as it was when it hits the server? 似乎在这个阶段更改href不会及时更新链接,并且URL将像它到达服务器时那样?

Is it possible to change a URL after it has been clicked or do I need to look at another method? 是否可以在点击后更改URL,还是需要查看其他方法?

Thanks 谢谢

You could redirect the window yourself, like this: 您可以自己重定向窗口,如下所示:

var holder = $('.hidden-information').first();                                   
var newOutlets = $('input[name="newoutlets"]', holder).val();
var queryDate = $('input[name="enddate"]', holder).val();
window.location.href = this.href + "?endDate=" + queryDate + "&newOutlets=" + newOutlets;
return false; //prevent default going-to-href behavior

Or, whatever is updating those hidden fields, update the link then instead of when it's clicked, for example: 或者,无论是更新的隐藏字段,更新的链接,然后 ,而不是当它被点击,例如:

$("#someField").change(function() {
  var holder = $('.hidden-information').first();                                   
  var newOutlets = $('input[name="newoutlets"]', holder).val();
  var queryDate = $('input[name="enddate"]', holder).val();                              
  $("a.black").attr("href", function() {
    return "/manager/TargetResults.csv" + "?endDate=" + queryDate + "&newOutlets=" + newOutlets;
  });
});

The main difference is this still allows normal click behavior to work, ctrl+click, etc. 主要区别在于这仍然允许正常的点击行为,ctrl + click等。

Just rounding out your options, you could put a span inside the link: 只需简化选项,您就可以在链接中添加一个span

<a href="/manager/TargetResults.csv" class="black"><span>Download Target Reports</span></a>

...and then hook the click event on the span , which will happen before the click on the link. ...然后将click事件挂钩到span ,这将在click链接之前发生。

you can make javascript redirect here:- 你可以在这里进行javascript重定向: -

    var holder = $('.hidden-information').first();                                   
    var newOutlets = $('input[name="newoutlets"]', holder).val();                                   
    var queryDate = $('input[name="enddate"]', holder).val();                                    
    var anchor = $(this);                                    
    var link = anchor.attr('href');                                   
    link = link + "?endDate=" + queryDate + "&newOutlets=" + newOutlets;
    anchor.attr('href', link);
   location.href=link;

ya someone can disable js but your code is completely based on js. 有人可以禁用js,但你的代码完全基于js。 Thanks 谢谢

Without having a href, the click will reload the current page, so you need something like this: 没有href,点击将重新加载当前页面,所以你需要这样的东西:

<a href="#" onclick="f1()">jhhghj</a>

Or prevent the scroll like this: 或者像这样阻止滚动:

<a href="#" onclick="f1(); return false;">jhhghj</a>

Or return false in your f1 function and: 或者在f1函数中返回false并且:

<a href="#" onclick="return f1();">jhhghj</a>

....or, the unobtrusive way: ....或者,不引人注目的方式:

<a href="#" id="abc">jhg</a>
<a href="#" id="myLink">jhhghj</a>

<script type="text/javascript">
  document.getElementById("myLink").onclick = function() {
    document.getElementById("abc").href="xyz.php"; `enter code here`
    return false;
  };
</script>

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

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