2017-06-28 4 views
0

これは私のコードですが、1,2,3,4,5,6,7,8は動作しますが、Ajaxの成功には9,10は使えません。テーブルの現在の行に#pcs_tdh_tsp

function giasalessanpham_nhap() 
{ 
    $('#customers2').find('tr').click(function(){ 
     var masp = $(this).find('#pcs_tdh_masp').val(); //1 
     var slsp = $(this).find('#pcs_tdh_slsp').val(); //2 
     var gnsp = $(this).find('#pcs_tdh_gnsp').val(); //3 
     var gssp = $(this).find('#pcs_tdh_gssp').val(); //4 
     var lnsp = numberWithCommas(gssp - gnsp); 
     var tttt = numberWithCommas(gssp * slsp); 
     $(this).find("#pcs_tdh_lnsp").css("color", "red"); //5 
     $(this).find("#pcs_tdh_tttt").css("color", "red"); //6 
     $(this).find('#pcs_tdh_lnsp').val(lnsp); //7 
     $(this).find('#pcs_tdh_tttt').val(tttt); //8 
     $.ajax({ 
      url:baseurl+"/laythongtinsanphamtheomaajax/", 
      type: "POST", 
      dataType: "json", 
      data: {masp : masp}, 
      success:function(data) { 
       thue = data['pcs_cl_pd_thue']; 
       $(this).find('#pcs_tdh_tsp').css("color", "red"); //9 
       $(this).find('#pcs_tdh_tsp').val(thue); //10 
      } 
     });//ajax 
    });  
} 
+3

'this'はもう'に関連していないのでclick'イベントが発生しますが、Ajax自体です。 – Oen44

+1

あなたはちょうど 'context:this'をajaxオプションとして使うことができます –

+0

ありがとう、男! –

答えて

3

あなたが前に、このようなsuccess AJAXコールバックにそれを得るローカル変数にあなたのthis値を保存する必要があります。

function giasalessanpham_nhap() 
{ 
    $('#customers2').find('tr').click(function(){ 
     //1,2 ... 8 
     var selector = $(this); //save 
     $.ajax({ 
      url:baseurl+"/laythongtinsanphamtheomaajax/", 
      type: "POST", 
      dataType: "json", 
      data: {masp : masp}, 
      success:function(data) { 
       thue = data['pcs_cl_pd_thue']; 
       //here you work with your selector 
       selector.find('#pcs_tdh_tsp').css("color", "red"); //9 
       selector.find('#pcs_tdh_tsp').val(thue); //10 
      } 
     });//ajax 
    });  
} 

本当に起こった何がsuccess AJAXコールバックの内側にあることですあなたは異なるコンテキストを持っているので、thisはあなたのの内部とは異なりますハンドラ。これはもう同じオブジェクトを参照していないので、あなたは$(「#のcustomers2」)の値を保存する必要があります

0

これを試してみてください。

function giasalessanpham_nhap() 
{ 
    $('#customers2').find('tr').click(function(){ 
     var masp = $(this).find('#pcs_tdh_masp').val(); //1 
     var slsp = $(this).find('#pcs_tdh_slsp').val(); //2 
     var gnsp = $(this).find('#pcs_tdh_gnsp').val(); //3 
     var gssp = $(this).find('#pcs_tdh_gssp').val(); //4 
     var lnsp = numberWithCommas(gssp - gnsp); 
     var tttt = numberWithCommas(gssp * slsp); 
     $(this).find("#pcs_tdh_lnsp").css("color", "red"); //5 
     $(this).find("#pcs_tdh_tttt").css("color", "red"); //6 
     $(this).find('#pcs_tdh_lnsp').val(lnsp); //7 
     $(this).find('#pcs_tdh_tttt').val(tttt); //8 
     var that = $('#customers2'); 
     $.ajax({ 
      url:baseurl+"/laythongtinsanphamtheomaajax/", 
      type: "POST", 
      dataType: "json", 
      data: {masp : masp}, 
      success:function(data) { 
       thue = data['pcs_cl_pd_thue']; 
       that.find('#pcs_tdh_tsp').css("color", "red"); //9 
       that.find('#pcs_tdh_tsp').val(thue); //10 
      } 
     });//ajax 
    });  
} 
関連する問題