[英]jQuery click event requires double click to work
I actually got some problems with my script: indeed it needs 2 clicks to work in a click event. 我的脚本实际上遇到了一些问题:实际上,单击事件需要2次单击才能起作用。 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.");}
});
});
As of jQuery 1.7, the .live() method is deprecated. 从jQuery 1.7开始,不推荐使用.live()方法。 you should Use .on() to attach event handlers.
您应该使用.on()附加事件处理程序。
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() {
to 至
$(document).on('click','#td_new', function() {
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. 然后打开firebug或在
success:
part中添加console.log('something')
-并确保ajax请求按照需要完成并且不会继续运行。
other then that we need some more code... 否则我们需要更多代码...
have fun. 玩得开心。
EDIT : 编辑 :
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: 我试图根据您上载的代码来了解您要执行的操作,但是无法对其进行测试,因此这里有一些见解供您参考:
class
not id
to declare them. id
class
声明它们。 add_hobby
as a class and not to the td
. add_hobby
而不是td
。 Here is a small demo of the usage of on() and appending code to your page: 这是on()用法的小演示,并将代码附加到您的页面:
Now feel free to direct me to your exact problem. 现在随时将我引向您的确切问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.