dtk31564
2012-03-21 08:57
浏览 52
已采纳

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

I used this code for sending and returning result.

<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>

addSpecialFlag.php

<?php
echo $_POST['info'];
?>

This code should return "Info" in "<-b class='special' ->" but no returning result. Where is the problem ? Thanks in advance !

图片转代码服务由CSDN问答提供 功能建议

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

   &lt; script type =“text / javascript”&gt;  
 $(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); 
}}); 
}); 
});  
&lt; / script&gt; 
 
&lt; b style =“cursor:pointer”class =“special”rel =“&lt;?= $ v ['number']。”/ *“。$ v ['vid'  ];?&gt;“&gt;特殊&lt; / b&gt; 
   
 
 

addSpecialFlag.php

 &lt;?php 
echo $ _POST ['info']; 
?&gt; 
   
 
 

此代码应在“&lt”中返回“Info” ; -b class ='special' - &gt;“ 但没有回复的结果。 问题出在哪儿 ? 提前致谢 !

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duansha8115 2012-03-21 09:02
    已采纳

    The problem should be that $(this) inside your success-handler is not the same as outside of the handler. Doing it like this should solve your problem:

    $(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);
                }});       
        }); 
    });    
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanqiang7631 2012-03-21 09:04

    <b>? Umad? Please use <strong>.

    I have tidied your code so the paranthesis match their line indents and adjusted your success handler. The issue was the success handler is in a different scope to the click function scope so you needed to refer to it in another way, i.e. by assigning it to another variable.

    $(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);
                }
            });
        });
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题