jQuery / JavaScript:减去当前值'直到'零'

时间:2012-11-30 06:26:45

标签: javascript jquery mysql html5 web

这是一个简单的;假设我有一个从数据库中提取值的脚本,每次用户单击一个按钮时,该值减少一个计数,如何防止该值降低到零以下,

 if(count>0){
           $.post(sessionStorage.server, { count:count ,request:"counter",user:sessionStorage.sname });
        }

if(count==0){
            alert("Your value is now zero");
            sessionStorage.clear();
            sessionStorage.msg="Logged off, ran out of clicks";
            window.location.href = "../index.html";
        }

3 个答案:

答案 0 :(得分:3)

试试这个

if(count==0){
            alert("Your value is now zero");
            sessionStorage.clear();
            sessionStorage.msg="Logged off, ran out of clicks";
            window.location.href = "../index.html";
        }
else if(count>0){
           $.post(sessionStorage.server, { count:count ,request:"counter",user:sessionStorage.sname });
        }

答案 1 :(得分:1)

替换这个

if(count==0){

这一个

if(count<=0){

答案 2 :(得分:1)

原始代码看起来不错,只是一些建议

1.add else将改善js性能

if(count>0){
       $.post(sessionStorage.server, { count:count ,request:"counter",user:sessionStorage.sname });
}
else if(count==0){
        alert("Your value is now zero");
        sessionStorage.clear();
        sessionStorage.msg="Logged off, ran out of clicks";
        window.location.href = "../index.html";
}

2.检查服务器端的计数,永远不要相信js验证