2016-08-12 148 views
-1

我遇到了一个奇怪的问题。 我有一个在点击链接时执行的功能。该函数通过ajax提供警报和数据库更新。 我刚刚注意到,如果我第一次点击链接后,警报会出现好几次。当我点击它的时候,它会被执行2次,当我点击3次时,它会被执行4次。 这是我的代码。Javascript单击脚本首次执行后多次运行

$("a.action").on('click', function(e){ 

       if(confirm('Are you sure you want to set Action to 1?')){ 



        var $index = $(this).closest('tr').index(); 
        var ids = <?php echo json_encode($ids); ?>; 

        table.row($(this).closest('tr')).remove().draw(); 
        console.log("Row deleted"); 


        //Set $action = 1; 
        var xmlhttp = new XMLHttpRequest(); 
        xmlhttp.onreadystatechange = function() { 
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
         { 
          console.log("Action set to 1"); 
         } 
        }; 
        xmlhttp.open("GET", "db_update.php?id="+ids[$index], true); 
        xmlhttp.send(); 

       } 
       return true; 

      }); 
+0

后您的html代码 – jonju

+1

你为什么不使用jQuery的Ajax,当你已经在使用jQuery的? –

+0

我的猜测是你在这个事件处理程序中有更多的代码。你能分享吗? – trincot

回答