jQuery click事件需要双击才能起作用 [英] jQuery click event requires double click to work
问题描述
我的脚本实际上遇到了一些问题:实际上,单击事件需要2次单击才能起作用。脚本是这样的:
I actually got some problems with my script: indeed it needs 2 clicks to work in a click event. The script is this:
$('td#td_new').live('click', function() {
$.ajax({
type: 'GET',
url: 'add_hobby.php',
data: { hobby: $('#ricerca_interests').val() },
success: function(msg) {
nuovo_hobby="<tr id='hobby' class='checked' selezionato='si' hobby_id='"+msg+"' ><td id='hobby' width='179px'><div id='nome_hobby'>"+$('#ricerca_interests').val()+'</div></td></tr>';
$(nuovo_hobby).appendTo("#interessilista");
$('tr#new_hobby').hide().remove();
},
error: function() {alert("Error. Try later.");}
});
});
推荐答案
从jQuery 1.7开始,.live()方法不推荐使用。您应该使用.on()附加事件处理程序。
As of jQuery 1.7, the .live() method is deprecated. you should Use .on() to attach event handlers.
似乎您的代码似乎并没有产生可能导致第二次点击的需要...但是尝试更改-
it doesn't seems that your code generate something that can cause a second click need... but try changing-
$('td#td_new')。live('click',function(){
至
$(document).on('click','#td_new' ,function(){$code>
然后打开Firebug或添加 console.log('something')$
成功:
部分中的c $ c>-并确保ajax请求按照需要完成并且不继续运行。
then open firebug or add a console.log('something')
in the success:
part - and make sure that the ajax request finishes as wanted and not continuing to run.
否则我们需要更多代码...
other then that we need some more code...
玩得开心。
编辑:
我试图根据您上传的代码了解您要执行的操作,但是
无法测试它,因此这里有一些见解可供您参考:
I tried to understand what you are trying to do according to the code you upload, but there is noway to test it, so here are some insights for you:
- 在头部添加源脚本(jquery + UI等)。
- 删除jquery.min ...源代码-您别d之所以这么做,是因为您以任何方式都包括了完整的脚本。
- 我建议升级到jQuery 1.9 ...您正在使用1.7.2
- 使用时在不同元素上使用相同的标识符,则应使用
class
而不是id
声明它们。 - 我建议您将click事件作为类附加到div
add_hobby
而不是td
。
- Include your source scripts (jquery + UI etc) in the head section.
- Remove jquery.min... source - you don't need it because you include the full script any way.
- I recommend upgrading to jquery 1.9... you are using 1.7.2
- When using same identifier over different elements you should use
class
notid
to declare them. - I recommend you to attach the click event to the div
add_hobby
as a class and not to thetd
.
以下是on()用法的小演示,并将代码附加到您的页面:
Here is a small demo of the usage of on() and appending code to your page:
现在可以直接将我引向您的确切问题。
Now feel free to direct me to your exact problem.
这篇关于jQuery click事件需要双击才能起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!