如何将用户输入保存在Cordova项目的本地存储中

时间:2019-04-24 18:42:00

标签: javascript input save

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <div id="ex"></div>
    <input id='sky' name='name'>
    <button onclick="myFunc()">Click me</button>
    <script>
    function myFunc() {
        var ex = document.getElementById('ex');
        var demo = document.getElementById('sky').value;
        var l = localStorage;
        l.saveData = demo;
        console.log(l.saveData);
        console.log('called me');
        ex.innerHTML = l.saveData;
    }
    </script>
</body>

</html

如何在我的cordova应用程序中使用纯js保存用户输入? 在下面的代码l.saveData中保存它,但刷新时未保存。如何使其工作?

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助

步骤1::将数据保存在本地存储中。将执行一些操作

引用Localstorage

步骤2:刷新页面时,如果退出

则从本地存储中检索数据

答案 1 :(得分:-1)

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <div id="ex"></div>
    <input id='sky' name='name'>
    <button onclick="myFunc()">Click me</button>
    <script>
    function myFunc() {
        var ex = document.getElementById('ex');

        ex.innerHTML = localStorage.getItem("data_name");


        var demo = document.getElementById('sky').value;
        localStorage.setItem("data_name", demo);
    }
    </script>
</body>

</html

https://www.w3schools.com/jsref/prop_win_localstorage.asp