【问题标题】:Keep the value on textbox after page reload页面重新加载后保留文本框上的值
【发布时间】:2021-03-15 04:54:50
【问题描述】:

我正在为一个课程项目复制一个杂货店网页,并且想知道即使在网页刷新后如何保持数量框中的值。

   <button type="button" id="subtract" onclick="decrease()">-</button>
   <input class="quantity-box" type="text" id="text" value="0">
   <button type="button" id="add" onclick="increase()">+</button>
<script>

function decrease(){
  var textBox = document.getElementById("text");
  if (textBox.value>0){
    textBox.value--;
  }
}


function increase(){
              var a = 1;
              var textBox = document.getElementById("text");
              textBox.value++;
}
</script>

注意:我可以使用 AJAX,但我对此并不熟悉,所以如果它包含在解决方案中,简要说明就足够了。 HTML/JAVASCRIPT/CSS/AJAX

【问题讨论】:

标签: javascript html css ajax


【解决方案1】:

你也可以使用 localStorage

function decrease(){
  var textBox = document.getElementById("text");
  if (textBox.value > 0){
    textBox.value--;
    localStorage.setItem('quantity', textBox.value);
  }
}

function increase(){
   var a = 1;
   var textBox = document.getElementById("text");
   textBox.value++;
   localStorage.setItem('quantity', textBox.value);
}

window.onload = function() {
  var textBox = document.getElementById("text");
  textBox.value = localStorage.getItem('quantity');
}

【讨论】:

    【解决方案2】:

    您可以使用cookies,创建设置和获取cookies两个功能,然后使用它们来设置cookies中数量的值,您需要在加载网页时获取数量cookie,因为您需要设置即使页面重新加载也是数量值。

    这里有一个例子,告诉你如何实现你想做的事情。 干杯...

    window.onload = function(){
      document.getElementById("text").value = getCookie("quantity");
    }
    
    function decrease() {
      var currentValue = document.getElementById("text").value;
      if (currentValue > 0) {
        document.getElementById("text").value = --currentValue;
        setCookie('quantity', currentValue);
      }
    }
    
    function increase() {
      var currentValue = document.getElementById("text").value;
      currentValue = currentValue? currentValue: 0;
      document.getElementById("text").value = ++currentValue;
      setCookie('quantity', currentValue);
    }
    
    
    function setCookie(name, value) {
      var d = new Date();
      var days = 10; // expires in days
      d.setTime(d.getTime() + (days*24*60*60*1000));
      var expires = "expires="+ d.toUTCString();
      document.cookie = name + "=" + value + ";" + expires + ";path=/";
    }
    
    function getCookie(name) {
      var name = name + "=";
      var decodedCookie = decodeURIComponent(document.cookie);
      var ca = decodedCookie.split(';');
      for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
          c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length);
        }
      }
      return "";
    }
    <button type="button" id="subtract" onclick="decrease()">-</button>
    <input class="quantity-box" type="text" id="text" value="0">
    <button type="button" id="add" onclick="increase()">+</button>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多