admin管理员组文章数量:1389779
i have a search field in which the user enters a letter and the ajax script in the background performs a search. The results are displayed live in a ul-li list. The user can click on the li which class is 'search_list_item'. After the click the username/id is copied into another div (the msg-receiver div).
I have a strange behaviour because in Safari everything works fine. In firefox the click-event seems to be fired two times, because i see the username two times in the #msg_receiver-div.
The code looks like this:
$(document).ready(function(){
$('#searchresult').on('click', 'li.search_list_item', function(){
var msg_receiver_content = $('#msg_receiver').html();
$('#msg_receiver').html(msg_receiver_content + ' <li class="single_receiver" id="single_receiver_'+$(this).attr('id')+'">'+$(this).find('span').html()+'</li>');
$('#single_receiver_'+$(this).attr('id')).hide().fadeIn(500);
});
});
Does anybody know why the code is called two times in FF and one time in Safari - i only click one time on the li in the search result.
Edit:
In the initial state the #msg_receiver-div is empty, but it seems like the statement $('#msg_receiver').html(
is performed before the variable msg_receiver_content
has some content and then the variable has the content which should be added afterwards...
i have a search field in which the user enters a letter and the ajax script in the background performs a search. The results are displayed live in a ul-li list. The user can click on the li which class is 'search_list_item'. After the click the username/id is copied into another div (the msg-receiver div).
I have a strange behaviour because in Safari everything works fine. In firefox the click-event seems to be fired two times, because i see the username two times in the #msg_receiver-div.
The code looks like this:
$(document).ready(function(){
$('#searchresult').on('click', 'li.search_list_item', function(){
var msg_receiver_content = $('#msg_receiver').html();
$('#msg_receiver').html(msg_receiver_content + ' <li class="single_receiver" id="single_receiver_'+$(this).attr('id')+'">'+$(this).find('span').html()+'</li>');
$('#single_receiver_'+$(this).attr('id')).hide().fadeIn(500);
});
});
Does anybody know why the code is called two times in FF and one time in Safari - i only click one time on the li in the search result.
Edit:
In the initial state the #msg_receiver-div is empty, but it seems like the statement $('#msg_receiver').html(
is performed before the variable msg_receiver_content
has some content and then the variable has the content which should be added afterwards...
- 1 seems weird. can you provide jsfiddle? – worenga Commented Jul 6, 2012 at 17:02
-
Tried to rebuild this, but the event gets fired only once. +1 for fiddle. And you could also use
.append()
instead of fetching the whole content and concat it with another string. – insertusernamehere Commented Jul 6, 2012 at 17:04 - I try to deliver a fiddle tomorrow. Time is short. I tried it with append and even then i get the content two times. Even more weird is the fact, that it occurs when opening the page the first time. After i hit reload and the whole page refreshed everything works fine... – Torben Commented Jul 6, 2012 at 17:11
- Hello, i found out that the $(document).ready(function(){ is fired twice when adding a console.log('init'); to it. But this happens only, if i open the window the first time! After i reload the page with CMD+R the event is fired once. Why is this happening? I included the .js script only once in the <head> section of the page. – Torben Commented Jul 8, 2012 at 9:34
3 Answers
Reset to default 6I have been struggling with the same issue. All events seem to work fine, the click
event however seems to be firing multiple times. Adding the .off()
before .on()
seems to solve this issue.
This is causing multiple click
events:
$("#select").on("click", function(event){
//do something
});
This is firing exactly one click
event. I have not been able to trigger multiple events so far with this:
$("#select").off().on("click", function(event){
//do something
});
You might need event.stopPropagation()
to prevent event bubbling (which is probably happening).
$(document).ready(function(){
$('#searchresult').on('click', 'li.search_list_item', function(event){
event.stopPropagation(); // stop event bubbling
var msg_receiver_content = $('#msg_receiver').html();
$('#msg_receiver').html(msg_receiver_content + ' <li class="single_receiver" id="single_receiver_'+$(this).attr('id')+'">'+$(this).find('span').html()+'</li>');
$('#single_receiver_'+$(this).attr('id')).hide().fadeIn(500);
});
});
You can set a locking variable, since this action takes time, it should lock out the second click event.
$(document).ready(function(){
var blockClick = false;
$('#searchresult').on('click', 'li.search_list_item', function(){
if(blockClick){
}else{
blockClick = true;
var msg_receiver_content = $('#msg_receiver').html();
$('#msg_receiver').html(msg_receiver_content + ' <li class="single_receiver" id="single_receiver_'+$(this).attr('id')+'">'+$(this).find('span').html()+'</li>');
$('#single_receiver_'+$(this).attr('id')).hide().fadeIn(500);
blockClick=false;
}
});
});
本文标签: javascriptjQueryon(39click39) event fires two times in FirefoxStack Overflow
版权声明:本文标题:javascript - jQuery - .on('click', ...) event fires two times in Firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744714377a2621314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论