简体   繁体   English

javascript正在递增的HTML输入标签值属性

[英]HTML input tag value attribute that javascript is incrementing

Hello i need help with saving changes to HTML input tag value attribute that javascript is incrementing. 您好,我需要帮助来保存对javascript正在递增的HTML输入标签值属性的更改。 The input field should be incremented one each time it clicks on the arguments that I'm up and running, and when I go into the exam, the element writes the corresponding number (from 6 to 7, from 7 to 8 etc), but when I refresh the page does not save the last entry But return to the starting number, do I need a php for this or... i wont code to work this way i upload image value goes from 6 to 7 and when i get back to page value is 7 not 6 again 每次单击我正在运行的参数时,输入字段应增加一个,当我参加考试时,元素将写入相应的数字(从6到7,从7到8等),但是当我刷新页面时,不保存最后一个条目,但返回到起始编号,我是否需要为此使用php或......我不会代码以这种方式工作,我上传的图像值从6变为7,当我回来时返回页面值再次是7而不是6

HTML code HTML代码

                    <form action="" method="post" id="cms" name="cms" enctype="multipart/form-data">
                  <div class="input-group">
                      <input type="file" name="fileToUpload" id="fileToUpload">
                      <input type="text" name="numberjpg" id="numberjpg" value='6'>
                      <input type="text" name="numberpng" id="numberpng" value='6'>
                      <input type="bottun" id="submit" name="submit" class="form-control" value="Submit" onclick="increase()" >
                   </div>
                </form>

js code js代码

     var x=document.getElementById('numberjpg')
         function increase(){
           x.value++;
            alert(document.getElementById('numberjpg').value);
            document.getElementById("numberjpg").setAttribute('value',document.getElementById('numberjpg').value);
         }

I apologize for the unclear question before 对于之前不清楚的问题,我深表歉意

You can use a Storage Web API like localStorage or sessionStorage. 您可以使用Storage Web API,例如localStorage或sessionStorage。 I use local in the example. 我在示例中使用local。 Below is an explanation of the code I added. 以下是我添加的代码的说明。

https://jsfiddle.net/6bujo188/1/ https://jsfiddle.net/6bujo188/1/

  var stored = +localStorage.getItem('boxvalue');

The above gets the value of any string saved as boxvalue and returns it to stored. 上面的代码获取保存为boxvalue的任何字符串的值,并将其返回存储。 It is prepended by a plus sign to perform shorthand type conversion to an integer. 它以加号开头,以将速记类型转换为整数。

     if(stored) {
     x.value = stored; 
     }

The above checks if there is a stored item, and if there is, it changes the value of the element stored as x. 上面的代码检查是否存在存储的项目,如果存在,则更改存储为x的元素的值。

    window.onbeforeunload = function(){
      localStorage.setItem('boxvalue', document.getElementById('numberjpg').value);
}

The above saves the variable before the window unloads. 上面在窗口卸载之前保存了变量。 For instance, if you refresh the page. 例如,如果刷新页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM