我有一个下载页面,每个文件的最大下载计数为5。每一次下载都链接到download.php?fileid。单击时,下载次数将与数据库中的1相减。但它只有在访问者刷新页面时才会更新。
现在,我想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>窗口打开正常,但重新加载不起作用?有人知道这个问题吗?
发布于 2013-06-12 22:25:47
解释
.amount不是好的类名!改用.downloadsleft吧!
由于.amount不存在,因此没有为变量设置的初始值currentValue。
解决方案
(JSFiddle)
将JavaScript代码中的所有.amount替换为.downloadsleft
备注:我还在base10中添加了一个parseInt()函数,用于将下载次数与一个数字进行比较。
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();
};
}); 发布于 2013-06-12 22:21:21
文本中缺少()
if ((".amount").text() == "1")您不需要使用查询再次获取信息,它已经在var newValue中:
if (newValue == 1)编辑:您的代码只适用于页面中的一个下载,因为您对所有下载都使用了相同的".amount“(我假设您的意思是".downloadsleft")。一种解决方案是使用前一个元素,而不是按类定位元素:
$('.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();
};
}); https://stackoverflow.com/questions/17067828
复制相似问题