Javascript $()。在查询中查找

Javascript $()。在查询中查找,javascript,jquery,ajax,Javascript,Jquery,Ajax,这是我的代码,1,2,3,4,5,6,7,8它可以工作,但当我进入ajax成功9,10无法工作时,它无法在表的当前行中检测到我的PC_tdh_tsp function giasalessanpham_nhap() { $('#customers2').find('tr').click( function(){ var masp = $(this).find('#pcs_tdh_masp').val(); //1 var slsp = $(this).fi

这是我的代码,1,2,3,4,5,6,7,8它可以工作,但当我进入ajax成功9,10无法工作时,它无法在表的当前行中检测到我的PC_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
    });     
}
您应该先将此值保存到局部变量中,然后再将其放入成功ajax回调中,如下所示:

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回调中,您有不同的上下文,因此这与单击处理程序中的上下文不同。

这是因为它不再引用相同的对象,您应该保存$'customers2'的值 试试这个:

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
    });     
}
因为这与单击事件无关,而是与ajax本身有关