Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用AJAX jquery单击文本后如何获取数据?_Php_Jquery_Ajax_Post - Fatal编程技术网

Php 使用AJAX jquery单击文本后如何获取数据?

Php 使用AJAX jquery单击文本后如何获取数据?,php,jquery,ajax,post,Php,Jquery,Ajax,Post,我使用此代码发送和返回结果 <script type="text/javascript"> $(document).ready(function() { $('.special').click(function(){ var info = $(this).attr("rel"); //$(this).html(sku); $.ajax({ type: "

我使用此代码发送和返回结果

<script type="text/javascript"> 
    $(document).ready(function() {
        $('.special').click(function(){
            var info = $(this).attr("rel");
            //$(this).html(sku);
            $.ajax({
                 type: "POST",
                 url:"../ajax/addSpecialFlag.php", 
                 async: false,
                 data: {info:info},
                success:function(result){
                    $(this).html(result);
                }});       
        }); 
    });    
</script>

<b style="cursor: pointer" class="special"  rel="<?=$v['number']."/*".$v['vid']; ?>">Special</b>

$(文档).ready(函数(){
$('.special')。单击(函数(){
var info=$(this.attr(“rel”);
//$(this.html(sku);
$.ajax({
类型:“POST”,
url:“../ajax/addSpecialFlag.php”,
async:false,
数据:{info:info},
成功:功能(结果){
$(this).html(result);
}});       
}); 
});    

此代码应在“”中返回“Info”,但不返回结果。问题在哪里?提前谢谢

问题应该是
$(this)
成功
处理程序内部与处理程序外部不同。这样做可以解决您的问题:

$(document).ready(function() {
    $('.special').click(function(){
        var $this = $(this);
        var info = $this.attr("rel");
        $.ajax({
             type: "POST",
             url:"../ajax/addSpecialFlag.php", 
             async: false,
             data: {info:info},
             success:function(result){
                $this.html(result);
            }});       
    }); 
});    

问题应该是
$(this)
success
-处理程序内部与处理程序外部不同。这样做可以解决您的问题:

$(document).ready(function() {
    $('.special').click(function(){
        var $this = $(this);
        var info = $this.attr("rel");
        $.ajax({
             type: "POST",
             url:"../ajax/addSpecialFlag.php", 
             async: false,
             data: {info:info},
             success:function(result){
                $this.html(result);
            }});       
    }); 
});    
?乌马德?请使用

我已经整理好了你的代码,使你的代码与它们的行缩进相匹配,并调整了你的成功处理程序。问题在于成功处理程序与单击函数的作用域不同,因此您需要以另一种方式引用它,即通过将其分配给另一个变量

$(document).ready(function () {
    $('.special').click(function () {
        var info = $(this).attr("rel");
        var _this = $(this);
        //$(this).html(sku);
        $.ajax({
            type: "POST",
            url: "../ajax/addSpecialFlag.php",
            async: false,
            data: {
                info: info
            },
            success: function (result) {
                _this.html(result);
            }
        });
    });
});
?乌马德?请使用

我已经整理好了你的代码,使你的代码与它们的行缩进相匹配,并调整了你的成功处理程序。问题在于成功处理程序与单击函数的作用域不同,因此您需要以另一种方式引用它,即通过将其分配给另一个变量

$(document).ready(function () {
    $('.special').click(function () {
        var info = $(this).attr("rel");
        var _this = $(this);
        //$(this).html(sku);
        $.ajax({
            type: "POST",
            url: "../ajax/addSpecialFlag.php",
            async: false,
            data: {
                info: info
            },
            success: function (result) {
                _this.html(result);
            }
        });
    });
});

您是否已使用Firebug或Chrome开发工具检查了请求?请尝试将
error
处理程序添加到
ajax
调用中。这可能会揭示出问题所在……您是否使用Firebug或Chrome开发工具检查了请求?尝试将
error
处理程序添加到
ajax
调用中。这可以让我们了解出了什么问题……这很重要。请注意,不是
$(this)
,而是
$this
可以调用任何您想要的名称,例如
this\u first\u this
。我将省略美元符号,否则它可能会与jquery通过
$
实现自身的特殊路径混淆。这很重要。请注意,不是
$(this)
,而是
$this
可以调用任何您想要的名称,例如
this\u first\u this
。我将省略美元符号,否则它可能会与jquery通过
$
实现自身的特殊路径相混淆。