2012-03-22 82 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"); 
     } 
    }); 
};