【问题标题】:jQuery / JavaScript: Subtract current 'value' till 'Zero'jQuery / JavaScript:减去当前的“值”直到“零”
【发布时间】:2012-11-30 14:48:52
【问题描述】:

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

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

【问题讨论】:

  • 您的代码对我来说似乎很好,什么不起作用?计数器如何递增。
  • 正如已经回答的,我猜你只是错过了else

标签: javascript jquery mysql html web


【解决方案1】:

试试这个

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 });
        }

【讨论】:

    【解决方案2】:

    替换这个

    if(count==0){
    

    这个

    if(count<=0){
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-04
        • 1970-01-01
        • 1970-01-01
        • 2015-04-06
        • 1970-01-01
        相关资源
        最近更新 更多