为什么 jQuery Asual Address 插件会触发更改事件两次?
您好,
我正在使用 jQuery Asual Address 插件: http://www.asual.com/jquery/地址/
由于某种原因,在 Firefox 和 IE(但不是 Chrome)中,当参数之一是 url(以“http://”开头)时,地址更改事件会触发两次。
这是一个小提琴示例: http://jsfiddle.net/5L6Ur/
单击“foo”链接展示了我的问题。非常感谢任何帮助。
代码:
$(function() {
$('a').click(function(e) {
e.preventDefault();
$.address.value($(this).attr('href'));
});
var changecount = 0;
$.address.change(function(event) {
$('span').html(changecount++);
});
});
<a href="?u=http://foo.bar">foo</a><br />
<a href="?u=foo.bar">bar</a><br />
<span></span>
Greetings,
I'm using the jQuery Asual Address plugin: http://www.asual.com/jquery/address/
For some reason, in Firefox and IE (but not Chrome), the address change event is firing twice when one of the parameters is a url (starts with "http://").
Here's a fiddle with an example: http://jsfiddle.net/5L6Ur/
Clicking on the "foo" link demonstrates my problem. Any help is greatly appreciated.
Code:
$(function() {
$('a').click(function(e) {
e.preventDefault();
$.address.value($(this).attr('href'));
});
var changecount = 0;
$.address.change(function(event) {
$('span').html(changecount++);
});
});
<a href="?u=http://foo.bar">foo</a><br />
<a href="?u=foo.bar">bar</a><br />
<span></span>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用internalChange 和externalChange 函数。
http://jsfiddle.net/5L6Ur/9/
Try using the internalChange and externalChange functions.
http://jsfiddle.net/5L6Ur/9/
我解决此问题的方法是设置
searching
变量。像这样:
当我作为用户触发它(通过提交表单)时,这可以防止双重ajax请求,但当我点击后退按钮或重新加载页面时运行它。希望有帮助。
PS 这些事件是特定于 Rails 的,但可以使用 JQuery Ajax 回调来完成相同的操作。
My workaround for this the problem is setting the
searching
variable.something like:
This prevents the double ajax requests when I trigger it as a user (by submitting a form) but runs it when I hit back button or reload the page. Hope that helps.
PS The events are rails specific, but the same could be accomplished using JQuery Ajax calbacks.
我花了很多时间来解决同样的问题。我已经缩小了原因,但除了回避之外我没有其他解决方案。该问题是由您在示例中创建的哈希值中的“=”引起的。我猜测这个问题与插件执行的解析有关,因为我用许多常见的分隔符(“=”、“&”、“|”、“:”、“;”重新创建了问题) )。您可以通过将这些麻烦的字符替换为其他字符(我使用“/”和“.”)来避免双重触发问题。
这是我所看到的症状的更具体解释。 IE 和 Firefox 中都会使用 .change() 方法或 .externalChange() 和 .internalChange 方法的组合来双重触发更改事件。如果使用 .change() 方法,则不会真正指示发生了什么,但使用内部和外部方法可以让您更深入地了解问题。内部更改事件似乎表现正常,仅在触发事件实际上是内部时触发。然而,外部事件似乎一直在触发,无论触发事件是内部的还是外部的。结果是外部事件“正确”触发,这意味着仅触发 externalChange() 方法。内部事件“不正确地”触发,导致双重触发——internalChange() 方法按预期触发,但 externalChange() 方法也会触发。
希望这有帮助。
I spent a lot of time troubleshooting this same issue. I've narrowed in on the cause, but I don't have a solution to offer, other than avoidance. The problem is caused by the "=" in the hash value you are creating in your example. I'm guessing the issue has to do with the parsing performed by the plugin, because I've recreated the problem with many common delimiter characters ("=", "&", "|", ":", ";"). You can avoid the double-firing problem by replacing these troublesome characters with others (I'm using "/" and ".").
Here's a more specific explanation of the symptoms I'm seeing. The double-firing of the change event happens in both IE and Firefox using either the .change() method or a combination of the .externalChange() and .internalChange methods. If using the .change() method, there's no real indication of what's happening, but using the internal and external methods allows you to see a little deeper into the problem. The internal change event seems to behave properly, only firing when the triggering event is actually internal. However, the external event seems to fire all the time, regardless of whether the triggering event is internal or external. The result is that external events fire "correctly," meaning that only the externalChange() method is fired. Internal events fire "incorrectly," result in double-firing -- the internalChange() method fires as expected, but the externalChange() method fires as well.
Hope this helps.
看起来 github 上该插件的最新版本(1.4)修复了这个问题。
这是使用最新插件的更新示例:
http://jsfiddle.net/5L6Ur/10/
Looks like the latest version of the plugin on github (1.4) fixes this.
Here's an updated example using the latest plugin:
http://jsfiddle.net/5L6Ur/10/
你不应该用 $(function () { }); 包装它。功能。然后就可以了
You should not wrap it with the $(function () { }); function. Then it works