2014-06-11 25 views
0

在嵌套函數外功能的元件I具有一個具有改變功能和Ajax內的各功能..訪問在jquery的

$(".spring_explorations").each(function() { 
     $("#" + this.id + " select").change(function() { 
      alert("t"); 
      $.ajax({ 
       type: 'POST', 
       url: ('/admin/applications/get_sections_for_modal'), //pass query string to server 
       data: { 
       exploration_id: $("#" + this.id + " select").val() 
       }, 
       success: function (response) { 
        $(this).parents('span').next().show(); 
        $(this).parents('span').next().find("select").html(response); 
        console.log(response); 
       }, 
       error: function (xhr, ajaxOptions, thrownError) { 
        console.log(thrownError); 
       } 
      }) 
     }); 
    }); 

我想訪問success片斷內spring_explorations類的$(this)元件ajax腳本。

我能夠通過添加context: this訪問change函數的元素,但是如何獲取外部元素?

謝謝先進。

編輯:

$(this).parents('span').next().show();等是不確定的,因爲也許this是AJAX本身不是spring_explorations類。

如果這是一個重複的問題,我很抱歉。我只是相當新的JavaScript,我認爲這是正確的地方問這個問題。如果您認爲這不合適,請關閉我的問題。

+3

這是許多問題的重複。 – Nit

+0

@Nit - 哪一個是規範的? –

回答

0

爲什麼你每次寫,你應該寫這樣的,如果你在評論mentined的要素爲:

$(".spring_exploration").on('change','select',function() { 
     var element = this; 
     alert("t"); 
     $.ajax({ 
      type: 'POST', 
      url: ('/admin/applications/get_sections_for_modal'), //pass query string to server 
      data: { 
      exploration_id: $(element).val() 
      }, 
      success: function (response) { 
       $(element).parents('span').next().show(); 
       $(element).parents('span').next().find("select").html(response); 
       $(element).closest(".spring_exploration") // you can access like this 
       console.log(response); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       console.log(thrownError); 
      } 
     }) 
    }); 

,現在你可以使用訪問closest

+0

我有動態表單域謝謝 –

+0

這種方式將他們工作 –

+0

這是一個更好的方式做到這一點,這將與'動態'字段 –

4

只需在變量中存儲this。它往往被命名爲thatme或其他類型的同義詞來表示這一點。

$(".spring_explorations").each(function() { 
    var that = this; 
     //^ store this into variable 
    $("#" + this.id + " select").change(function() { 
     alert("t"); 
     $.ajax({ 
      type: 'POST', 
      url: ('/admin/applications/get_sections_for_modal'), //pass query string to server 
      data: { 
      exploration_id: $("#" + this.id + " select").val() 
      }, 
      success: function (response) { 
       $(that).parents('span').next().show(); 
       //^ re-use stored `this` value 
       $(that).parents('span').next().find("select").html(response); 
       console.log(response); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       console.log(thrownError); 
      } 
     }) 
    }); 
}); 
+0

我會嘗試對不起,只是一個小號在JavaScript太 –

0

您需要從您想在使用它的上下文引用this。你可以嵌套作用域,其中作爲this值將根據範圍發生變化,因爲你已經發現了內使用引用。

$(".spring_explorations").each(function() { 
    var _this = this; 
    // You now have a reference to the node in the variable _this 
    // that can be used anywhere within this functions scope 

    $("#" + this.id + " select").change(function() { 
     var self = this; 
     // Self is now a reference to the changed element 
     // that can be used within this functions scope 

     // your code here that uses _this and self when you please. 
    }); 
});