简体   繁体   English

jQuery click事件需要双击才能起作用

[英]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: 我试图根据您上载的代码来了解您要执行的操作,但是无法对其进行测试,因此这里有一些见解供您参考:

  1. Include your source scripts (jquery + UI etc) in the head section. 在头部添加您的源脚本(jquery + UI等)。
  2. Remove jquery.min... source - you don't need it because you include the full script any way. 删除jquery.min ...源代码-不需要它,因为您可以通过任何方式包含完整脚本。
  3. I recommend upgrading to jquery 1.9... you are using 1.7.2 我建议升级到jQuery 1.9 ...您正在使用1.7.2
  4. When using same identifier over different elements you should use class not id to declare them. 当在不同元素上使用相同的标识符时,应使用非id class声明它们。
  5. I recommend you to attach the click event to the div add_hobby as a class and not to the td . 我建议您将click事件作为类附加到div add_hobby而不是td

Here is a small demo of the usage of on() and appending code to your page: 这是on()用法的小演示,并将代码附加到您的页面:

jsfiddle Demo jsfiddle演示

Now feel free to direct me to your exact problem. 现在随时将我引向您的确切问题。

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

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