【问题标题】:get css this and value selector from jquery ajax success data从 jquery ajax 成功数据中获取 css this 和 value 选择器
【发布时间】:2021-12-28 18:46:38
【问题描述】:

如何从内部 ajax 成功设置访问所需的选择器类?

示例: 具有类“formdataaj”的内部表单位于一个具有类“img_ajx”的div, 我想通过 ajax 响应写文本。

我的问题是我无法通过 $(this) 访问选择器。如果我把固定类 $('.formdataaj .img_ajx').text(jsondata.image);工作没有问题,但显然只在一流的“formdataaj”中。

谢谢

我的代码:

$('.formdataaj').each(function(){
        $(this).on('submit',function(e){
        e.preventDefault();
        var form_data = new FormData(this);
        //console.log(...form_data);
        $.ajax({
            type: 'post',
            url: 'php/extra_update.php',
            data: form_data,
            processData:false,
            contentType:false,
            cache: false,
            success: function(data)
            {
                var jsondata = $.parseJSON(data);
                console.log(jsondata);
                $(this).find(".img_ajx").text(jsondata.image);
            }
        })
        
    })
    })

【问题讨论】:

    标签: javascript jquery css ajax


    【解决方案1】:
    // you need to asign [this keyword] to variable out of success scope 
    $('.formdataaj').each(function(){
       var $this = $(this);
            $(this).on('submit',function(e){
            e.preventDefault();
            var form_data = new FormData(this);
            //console.log(...form_data);
            $.ajax({
                type: 'post',
                url: 'php/extra_update.php',
                data: form_data,
                processData:false,
                contentType:false,
                cache: false,
                success: function(data)
                {
                    var jsondata = $.parseJSON(data);
                    console.log(jsondata);
                    $this.find(".img_ajx").text(jsondata.image);
                }
            })
            
        })
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多