按回车键或页面刷新后,在页面后保留 jQuery 值



我想在按回车键或刷新不重新加载后为输入框值保留一个 jQuery 值。 单击按钮时,然后设置输入框的值,但是当页面刷新或按回车键时,输入框值擦除。 我使用本地存储,但它对我不起作用。

这是我的 HTML 代码....

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

这是我的jquery.....

$(function(){
       $('#read').on('click', function () {
           var someVarName = "value";
           localStorage.setItem("someVarName", someVarName);
           var someVarName = localStorage.getItem("someVarName");
           $('#value').val(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>

最新更新