简体   繁体   English

jQuery绑定单击不添加元素

[英]jQuery bind click not adding elements

I have a button that when I click it more than once it is adding elements from the previous click. 我有一个按钮,当我多次单击它时,它将添加上一次单击中的元素。 It works fine the first time through. 第一次运行正常。 We are using jQuery 1.11.1. 我们正在使用jQuery 1.11.1。

$(function() {
  $('a[name="generateReport"]').bind('click', function() {
    $(this).attr('href', $(this).attr('href') + '?est=' + est.value + '&prodcd=' + prodcd.value + '&startDate=' + startDate.value + '&endDate=' + endDate.value + '&tracking=' + tracking.value);
  })
})

What I am seeing is that the URL past to the server is adding the fields from the prior click. 我看到的是,服务器的URL正在添加来自先前单击的字段。 This of course causes issues when it gets to the server. 当到达服务器时,这当然会引起问题。 Does this need to be cleared out after each click? 每次点击后是否需要清除?

This is the code that calls it from our Grails app(2.4.3) 这是从我们的Grails应用程序调用它的代码(2.4.3)

<g:link class="btn btn-primary" name="generateReport" controller="generateTTLReport" action="renderOutput">
                                <i class="icon-print icon-white"></i>
                                    Generate Report
                            </g:link>

Thanks, 谢谢,

Tom 汤姆

The problem with your code is, every time you click on that button, bind callback is invoked. 代码的问题是,每次您单击该按钮时,都会调用bind回调。 So the first time you clicked, it got the href attribute added some parameters and replaced it. 因此,第一次单击时,它使href属性添加了一些参数并替换了它。 Again when you clicked on that for the second time, it does the same thing. 同样,当您第二次单击该按钮时,它会执行相同的操作。 It gets the href attribute which now contains parameters from previous update and then replace the existing. 它获取href属性,该属性现在包含来自先前更新的参数,然后替换现有参数。 If you keep the href as it is, and only update the query parameters, you can define that as a global variable and use that in your event handler 如果您保持href不变,并且仅更新查询参数,则可以将其定义为全局变量,并在事件处理程序中使用它

You can hard code that link as a variable within in your script like this 您可以像这样在脚本中将链接作为变量硬编码

$(function() {

  // define your variable within document.ready but outside your event handler
  var reportURL = '/LSRTIS/generateTTLReport/renderOutput';

  $('a[name="generateReport"]').bind('click', function() {
    var urlWithParams = reportURL + '?est=' + est.value + '&prodcd=' + prodcd.value + '&startDate=' + startDate.value + '&endDate=' + endDate.value + '&tracking=' + tracking.value;
    $(this).attr('href', urlWithParams );
  });
}); 

Hope this helps :) 希望这可以帮助 :)

Split the current href at the "?" 在“?”处拆分当前href。 to remove the query string parameters. 删除查询字符串参数。 Also, let jQuery build your new query string parameter string. 另外,让jQuery构建新的查询字符串参数字符串。

$(function() {
    $('a[name="generateReport"]').on('click', function() {
        var $this = $(this),
            path = $this.attr('href').split('?')[0],
            params = {
                est: est.value,
                prodcd: prodcd.value,
                startDate: startDate.value,
                endDate: endDate.value,
                tracking: tracking.value
            },
            href = [path, $.param(params)].join('?');

        $this.attr('href', href);
    });
});

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

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