ProgramingTip

$ .ajax 선택 옵션

bestdevel 2020. 11. 30. 19:25
반응형

$ .ajax 선택 옵션


yayQuery 팟 캐스트 의 에피소드 11 에는 $ 아약스 옵션이 컨텍스트 언급되어 있습니다 . 성공 에서이 옵션을 어떻게 사용합니까? 내가 현재하고있는 것은 성공 / 오류 후에 호출 된 ID를 애니메이션 할 수 있도록 입력 된 변수를 성공에 다시 전달하는 것입니다. 사용 옵션을 사용하면 호출 된 루틴에서 다시 시작 필요가 없습니다.

이 예제에서는 데이터베이스에서 삭제 된 상태가 DOM에서 제거 된 STATEID를 성공 필드에 다시 전달합니다.

$('td.delete').click(function() {
  var confirm = window.confirm('Are you sure?');
  if (confirm) {
    var StateID = $(this).parents('tr').attr('id');
    $.ajax({
      url: 'Remote/State.cfc',
      data: {
        method: 'Delete',
        'StateID': StateID
      },
      success: function(result) {
        if (result.MSG == '') {
          $('#' + result.STATEID).remove();
        } else {
          $('#msg').text(result.MSG).addClass('err');;
        };
      }
    });
  }
});

모든 작업은 절약 context의 값을 설정하는 것 this입니다.

그럴 경우에는 이벤트 this를 수신 할 수 있습니다.

context:this,
success:function() {
    // "this" is whatever the value was where this ajax call was made
}

다른 유형을 원할 경우 설정하고 this참조하십시오.

context:{some:'value'},
success:function() {
    // "this" the object you passed
    alert( this.some ); // "value"
}

질문에 추가 한 코드를 사용할 수 있도록 이미 필요한 코드 StateID에 액세스 할 수 있습니다.

var StateID = $(this).parents('tr').attr('id');
$.ajax({
    url: 'Remote/State.cfc'
    ,data: {
        method:'Delete'
        ,'StateID':StateID
    }
    ,context: StateID
    ,success: function(result){

        alert(this);     // the value of StateID
        alert(StateID);  // same as above

        if (result.MSG == '') {
            $('#' + result.STATEID).remove();
        } else {
            $('#msg').text(result.MSG).addClass('err');;
        };
    }
});

온라인 옵션 this을 설정하면 대한 값으로 설정 한 값이 성공합니다 context. 따라서 입력 된 매개 변수 이름과 값을 입력로 포함하는 객체 리터럴을 전달하면 this.param1첫 번째 입력 매개 변수의 값을 가져 오는 데 사용할 수 있습니다 .

자세한 내용은 .ajax () 문서를 참조하십시오.

참고 URL : https://stackoverflow.com/questions/5097191/ajax-context-option

반응형