【问题标题】:how to change the text in element via ajax?如何通过ajax更改元素中的文本?
【发布时间】:2012-10-12 18:48:12
【问题描述】:

我有一个下载按钮,点击后应该会更改元素跨度的文本,但我在 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);

【问题讨论】:

  • 点击后会显示一个小按钮,文字不会改变
  • 执行 alert(html) 或 console.log(html) 会得到什么?
  • 它没有显示我现在尝试的警报框
  • download_number.php 文件中有什么内容?
  • @nikparsa:那么success 回调没有运行。试试error 而不是success

标签: php jquery html ajax


【解决方案1】:

您不应该在&lt;button&gt; 中添加&lt;a&gt; - 链接将胜过按钮操作 [see fiddle]。

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

【讨论】:

  • 但我想下载我的文件删除会丢失我的文件
  • 那你为什么需要一个链接和一个按钮呢?坚持一个,你就完成了^^
  • 好的,我认为您需要它来进行统计 - 不是通过 vanilla apache 提供文件,而是通过 PHP 处理它们,保存您的统计数据,然后转发二进制 mp3 数据。
  • 点击按钮时我希望用户下载文件并使用ajax同时更新下载数
  • 我不知道香草?!我正在使用 wamp apache,整个都是 php 你是什么意思?
【解决方案2】:

把数据串改成这个

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

并添加datatpe json...这样做可能会起作用....只是猜测

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

              } 

【讨论】:

  • 它什么也没显示,一秒钟后显示我的文件准备好下载
  • 数据串肯定没有问题
猜你喜欢
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 2020-09-18
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
相关资源
最近更新 更多