如何通过ajax更改元素中的文本?

时间:2012-10-12 18:48:13

标签: php jquery html ajax

我有一个下载按钮,点击它时应该更改元素跨度的文本但我在ajax部分有问题

点击后会显示一个小按钮,文字不会改变

这是我的HTML

//somecode
    <span class="num">Downloaded:<?php echo $downloadcount;?></span>
     </td></tr>
     </table>
     </div>
<button type = "button" class="button" id="<?php echo $id; ?>" name="dl">
    <a href='./mp3/<?php echo basename($filename);?>'>Download</a>
</button>
         </td>
     </tr>
    </table>

这是我的ajax:

    $(function() {
        $(".button").click(function() 
        {
        var id = $(this).attr("id");
        var name = $(this).attr("name");
        var dataString = 'id='+ id ;
        var parent = $(this);
        if(name=='dl')
        {
        $(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
        $.ajax({
           type: "POST",
           url: "download_number.php",
           data: dataString,
           cache: false,

           success: function(html)
           {
//THE PROBLEM IS HERE
            $(".num").text(html);

          }  });

        }
        });
        });

我也试过$(".num").html(html);

2 个答案:

答案 0 :(得分:2)

你不应该在<a>内加<button> - 该链接将胜过按钮操作[see fiddle]。

除此之外,您的代码似乎正确。

答案 1 :(得分:0)

将数据字符串更改为此

var dataString = "{'id'="+ id+"}" ;  

并添加数据类型json ...通过这样做它可能工作....只是猜测

datatype:'json', 
  success: function(html)
               {
               alert(html);
               // First see...what u r getting in response and then move forward

              }