2016-03-17 37 views
0

我一直在尋找這個問題我現在面臨的網絡上..

我無法找到要使用的方式,在我的第二個嵌套循環,我的第一個$(this)。 我想像Php那樣做別名?

有人知道是否有可能嗎?

你可以在下面看到我的代碼來表示問題。

$(".btn-labeled").click(function() 
{ 
    var uid = $(this).parent().prev("td").html(); 
    $.get("?what="+uid, function(data) { 
    var parsedJson = JSON.parse(data); 

    $(".rule").each(function() 
    { 
    var serviceAccount = $(this).children("td").html(); 
    parsedJson.forEach(function(iteration) 
    { 
     if(iteration["service_account"] != serviceAccount) 
     { 
     //I want this to be attached to first loop :p 
     $(this).children("td").next("td").children("div").children("input").removeAttr("checked"); 
     } 
    }); 

    }); 


    }); 
}); 

預先感謝

回答

2

創建一個變量element參考的是,在內部循環:

$(".btn-labeled").click(function() 
{ 
    var uid = $(this).parent().prev("td").html(); 
    $.get("?what="+uid, function(data) { 
    var parsedJson = JSON.parse(data); 

    $(".rule").each(function() 
    { 
    var element = $(this);//here 
    var serviceAccount = $(this).children("td").html(); 
    parsedJson.forEach(function(iteration) 
    { 
     if(iteration["service_account"] != serviceAccount) 
     { 
     element.children("td").next("td").children("div").children("input").removeAttr("checked"); 
     } 
    }); 

    }); 


    }); 
}); 
+1

感謝您的回覆,現在很清楚! –

1

可以設置外上下文對象作爲變量,然後內部使用它:

$(".rule").each(function(){ 
var obj= $(this); 
var serviceAccount = $(this).children("td").html(); 
parsedJson.forEach(function(iteration) 
{ 
    if(iteration["service_account"] != serviceAccount) 
    { 
    //I want this to be attached to first loop :p 
    obj.children("td").next("td").children("div").children("input").removeAttr("checked"); 
    } 
}); 
}); 
1

只需將$(this)分配給變量,然後在那裏使用那個變量。

var $this = $(this);

$(".btn-labeled").click(function() 
{ 
    var uid = $(this).parent().prev("td").html();  

    $.get("?what="+uid, function(data) { 
    var parsedJson = JSON.parse(data); 

    $(".rule").each(function() 
    { 

    var $this = $(this); // assign $(this) to a new variable 

    var serviceAccount = $(this).children("td").html(); 
    parsedJson.forEach(function(iteration) 
    { 
     if(iteration["service_account"] != serviceAccount) 
     { 
     //Use that variable 
     $this.children("td").next("td").children("div").children("input").removeAttr("checked"); 
     } 
    }); 

    }); 


    }); 
}); 
1

this在Javascript範圍是動態範圍。這取決於該函數的調用,而不是如何筆者(在詞法範圍等)編寫一行代碼

所以,你的代碼應該是這樣的

$(".btn-labeled").click(function() 
{ 
    var uid = $(this).parent().prev("td").html(); 
    $.get("?what="+uid, function(data) { 
    var parsedJson = JSON.parse(data); 
    // save reference to this also it is good idea to prefix variables containing jQuery objects with a $ 
    var $outer_this = $(this) 
    $(".rule").each(function() 
    { 
    var serviceAccount = $outer_this.children("td").html(); 
    parsedJson.forEach(function(iteration) 
    { 
     if(iteration["service_account"] != serviceAccount) 
     { 
     //I want this to be attached to first loop :p 
     $outer_this.children("td").next("td").children("div").children("input").removeAttr("checked"); 
     } 
    }); 

    }); 


    }); 
}); 

的內部函數可以訪問外因爲內部函數關閉了外部函數的變量。