Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按enter键或页面刷新后在页面后保留jquery值_Javascript_Jquery_Local Storage - Fatal编程技术网

Javascript 按enter键或页面刷新后在页面后保留jquery值

Javascript 按enter键或页面刷新后在页面后保留jquery值,javascript,jquery,local-storage,Javascript,Jquery,Local Storage,我想在按enter键或刷新不重新加载后为输入框值保留一个jquery值。当点击按钮,然后设置一个输入框的值,但当页面刷新或按下回车键,然后输入框值擦除。我使用过localStorage,但它不适合我 这是我的html代码 <button id="read">set value</button> <input type="text" id="value"> 下面是工作代码 <html> <head> <script src="ht

我想在按enter键或刷新不重新加载后为输入框值保留一个jquery值。当点击按钮,然后设置一个输入框的值,但当页面刷新或按下回车键,然后输入框值擦除。我使用过localStorage,但它不适合我

这是我的html代码

<button id="read">set value</button>
<input type="text" id="value">

下面是工作代码

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
        $('#read').on('click', function () {
           var someVarName = "value";
           localStorage.setItem("someVarName", someVarName);
           var someVarName = localStorage.getItem("someVarName");
           $('#value').val(someVarName)
       });
});
</script>
</head>
<body>
<button id="read">set value</button>
<input type="text" id="value">
</body>
</html>

$(文档).ready(函数(){
$('read')。在('click',函数(){
var someVarName=“value”;
setItem(“someVarName”,someVarName);
var someVarName=localStorage.getItem(“someVarName”);
$('#value').val(someVarName)
});
});
设定值

console中有错误吗?\n是否重定向到同一域?或者重定向发生在不同的网站上,而不是“someVarName”,请您提及您在页面中使用的正确变量名。这可能是问题的一种可能性。当进行第二次刷新/提出新请求时,我如何删除它?
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
        $('#read').on('click', function () {
           var someVarName = "value";
           localStorage.setItem("someVarName", someVarName);
           var someVarName = localStorage.getItem("someVarName");
           $('#value').val(someVarName)
       });
});
</script>
</head>
<body>
<button id="read">set value</button>
<input type="text" id="value">
</body>
</html>