php - $_post的返回函数中能否操作html元素?
巴扎黑
巴扎黑 2017-04-11 09:00:06
0
5
455

我的问题:

 $.post(url, function(result)
        {
            if (result=="1")
            {//这个if语句能进入,已测试成功
                $(this).parents('.p1').remove();//问题所在?
                return false;
            }
        }, 'json');
        
 

: $(this).parents('.p1').remove(); 放在$_post之外也能测试成功。

为什么放在$_post中无法测试成功?

巴扎黑
巴扎黑

Antworte allen(5)
洪涛

$(this)作用域的问题,可以这样

var _this = $(this);
 $.post(url, function(result)
        {
            if (result=="1")
            {//这个if语句能进入,已测试成功
               _this.parents('.p1').remove();
                return false;
            }
        }, 'json');
PHPzhong

这个是问题是不管POST,GET,AJAX 你往服务器传值,限定了返回值的类型,如果返回不是json类型,接收不到 但是放在外面是可以执行的,就是说 你在服务器端的代码不是 json_encode() 返回类型的。

PHPzhong

经典的 this 的问题么

伊谢尔伦

看具体报错信息,$(this)作用域肯定有问题。
如果result的值是1那肯定没问题,如果是json或其他,最后的判断肯定也要改。

迷茫

首先 $(this) 这个作用域有问题 可以定义在 post之外 第二个 如果ajax放回时间过长 页面中没有效果 可以改为同步ajax 体验会更好

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!