php jquery ajax json发表链接attr title?

时间:2011-05-09 22:43:34

标签: php jquery ajax json post

我尝试使用jquery ajax json post发布链接标题属性中的值。这是我的代码。问题出在哪儿?为什么不起作用?

main.php

<script type="text/javascript">     
    $(document).ready(function(){
        $(".link").click(function(){
        var aa = $(this).attr('title'); 
          $.ajax({
             url: "data.php", 
             dataType: "json", 
               data: "number1="+aa, 
             success: function(json){ 
                $("#result").html(json.number1); 
             }
          });
        });
    });
    </script>
      <a href="#" class="link" title="apple">A</a>
      <a href="#" class="link" title="banana">B</a>
      <a href="#" class="link" title="orange">O</a>
        <div id="result"></div>

data.php

<?php
$number1 = $_GET['number1'];
echo json_encode($number1);
?>

1 个答案:

答案 0 :(得分:2)

试试这个:

<script type="text/javascript">     
    $(document).ready(function(){
        $(".link").click(function(){
        var aa = $(this).attr('title'); 
          $.ajax({
             url: "data.php", 
             dataType: "json", 
               data: {"number1": aa}, 
             success: function(json){ 
                $("#result").html(json.number1); 
             }
          });
        });
    });
    </script>
      <a href="#" class="link" title="apple">A</a>
      <a href="#" class="link" title="banana">B</a>
      <a href="#" class="link" title="orange">O</a>
        <div id="result"></div>


<?php
$number1 = $_GET['number1'];
echo json_encode(array('number1' =>$number1));
?>