0

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

 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";
        }
4

3 回答 3

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 });
        }
于 2012-11-30T06:33:39.250 回答
1

orig代码看起来不错,只是一点建议

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验证

于 2012-11-30T06:48:52.140 回答
1

替换这个

if(count==0){

有了这个

if(count<=0){
于 2012-11-30T06:29:11.337 回答