2012-03-22 80 views
0

在我的页面中,我需要将当前变量作为参数赋予函数AjaxLoader,然后向其中添加选定的类。 但是,当我将它作为参数传递它不起作用。有没有办法做到这一点? 我的代码如下。在jquery中添加这个参数作为参数

$('a').click(function() { 

    var current = $(this).find('td'); 
    current.addClass("selected"); 
    AjaxLoader(current); 

} 

function AjaxLoader(current){ 

    $.ajax({ 
    type: "POST", 
    url: "test1.php", 
    success: function(response) { 
    //this is what I want to do 
    current.addClass("selected"); 
    } 


    }) 


} 
+0

您收到了什么错误元素的后裔?在添加课程以查看内容之前,您是否曾尝试将'current'记录到控制台? – slash197 2012-03-22 12:49:39

回答

0

您没有关闭您的功能。你也可以在使用它之前定义这个函数。

function AjaxLoader(current){ 

    $.ajax({ 
     type: "POST", 
     url: "test1.php", 
     success: function(response) { 
      //this is what I want to do 
      current.addClass("selected"); 
      } 
    }); 
} 

$('a').click(function() { 

    var current = $(this).find('td'); 
    current.addClass("selected"); 
    AjaxLoader(current); 

}); 
+0

“AjaxLoader”函数已经关闭。你已经添加了一个额外的,无效的');'。不过,在'click'事件处理程序中发现了它。 – 2012-03-22 12:54:16

+0

哎呀,现在更正 – 2012-03-22 12:55:09

+1

很酷。但是,在使用它之前,您不必声明该函数,因为函数声明被提升到它们声明的范围的顶部。它确实使代码更容易遵循。 – 2012-03-22 12:56:05

0

这是极不可能的,你有一个标签内的表。 您可能想要closest()parent()而不是find()以获取包含A标记的td。 find()查找

http://api.jquery.com/closest/

http://api.jquery.com/parent/

http://api.jquery.com/parents/

$('a').click(function() { 

    var current = $(this).closest('td'); 
    current.addClass("selected"); 
    AjaxLoader(current); 

}); 

function AjaxLoader(current){ 

    $.ajax({ 
     type: "POST", 
     url: "test1.php", 
     success: function(response) { 
     //this is what I want to do 
     current.addClass("selected"); 
     } 
    }); 
};