最大下载次数 - >然后刷新页面

时间:2013-06-12 14:14:43

标签: jquery html page-refresh

我有一个下载页面,每个文件的最大下载次数为5。每次下载都链接到download.php?fileid。单击时,下载数量将减去数据库中的一个。但只有在访问者刷新页面时才会更新。

现在,我认为jQuery会帮助我,现在我有了这个:

<span class="downloadsleft">5</span><span class="downloadLink">download file</span>

我的jquery:

<script type="text/javascript">     
                $('.downloadLink').click(function() {

                        var currentValue = $(".amount").text();
                        var newValue = +currentValue - 1;

                        $(".amount").text(newValue);

                        window.open("download.php?file={/literal}{$product.bestandsnaam_hash}{literal}");                   

                        if ((".amount").text == "1")
                        {
                            location.reload();
                        };
                });  

            </script>

窗口打开,但重装不起作用?有谁知道这个问题?

2 个答案:

答案 0 :(得分:2)

你缺少文本中的()

 if ((".amount").text() == "1")

您不需要再次使用查询获取信息,它已经在var newValue中了:

 if (newValue == 1)

编辑:您的代码只适用于页面中的一次下载,因为您使用相同的“.amount”(我假设您的意思是“.downloadsleft”)来进行所有下载。一种解决方案是使用前面的元素intead按类定位元素:

$('.downloadLink').click(function() {
      var counter = $(this).prev();
      var newValue = counter.text() - 1;
      counter.text(newValue);
      window.open("download.php?file={/literal}{$product.bestandsnaam_hash}{literal}");                   
      if (newValue == 1) {
          location.reload();
      };
}); 

答案 1 :(得分:1)

解释

.amount不是好名字!改为使用.downloadsleft

由于.amount不存在,因此对变量currentValue 没有设置初始值


解决方案

JSFiddle

.amount代码中的所有JavaScript替换为.downloadsleft

注意:我还在parseInt()中添加了base10功能,以将下载次数与数字进行比较。

JavaSript / jQuery的

  $('.downloadLink').click(function() {
      var currentValue = $(".downloadsleft").text();
      var newValue = currentValue - 1;

      $(".downloadsleft").text(newValue);

      window.open("download.php?file={/literal}{$product.bestandsnaam_hash}{literal}");                

      if (parseInt(newValue,10) == 1)
      {
          location.reload();
      };
  });