2014-06-11 2 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); 
       } 
      }) 
     }); 
    }); 

I는 success 스니 spring_explorations 클래스 $(this) 요소에 액세스하고자 ajax 스크립트입니다.

context: this을 추가하여 change 함수의 요소에 액세스 할 수 있었지만 어떻게하면 외부 요소를 얻을 수 있습니까?

고맙습니다.

편집 : 아마 this가 아약스 자체가 아니라 spring_explorations 클래스이기 때문에

$(this).parents('span').next().show(); 등이 정의되지 않습니다.

중복 된 질문 인 경우 대단히 죄송합니다. 나는 자바 스크립트에서 상당히 새롭다. 그리고 나는 이것이이 것에 대해 물어볼 적절한 장소라고 생각한다. 이것이 적절하지 않다고 생각되면 제 질문을 닫으십시오.

+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을 변수에 저장하십시오. 이것을 나타내는 데는 that 또는 me 또는 다른 유형의 동의어로 사용되는 경향이 있습니다.

$(".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

미안하지만 자바 스크립트에서 멍청한 답변을 시도합니다 –

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. 
    }); 
}); 
관련 문제