首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript Localstorage:将输入值与localstorage中数组中存储的值进行比较

Javascript Localstorage:将输入值与localstorage中数组中存储的值进行比较
EN

Stack Overflow用户
提问于 2018-07-01 05:50:37
回答 1查看 988关注 0票数 0

我的目的是检查用户在输入字段中输入的值在存储在本地存储中的数组中是否已经可用。如果是,则打印数组,如果没有将新值添加到存储器中。我正在让我的数组在按钮点击时返回,但是代码不能正常工作。输出为:"hh","try","vogue","vogue","try2","try2“

上面是重复添加的输入值。我知道这是一个愚蠢的问题,但在处理本地存储中的数组方面经验最少。任何帮助都将不胜感激。(我在stackoverflow上尝试了类似问题中提供的解决方案,但没有成功)

代码语言:javascript
复制
<html class=" reevoomark_ATY">
    <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Special Offers</title>
        <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="crossorigin="anonymous"></script>
    </head>
<body>
    <input id="coupon"> <button style="width:50px;padding:10px;background:grey;height:30px;border:1px solid grey" id="button">Apply</button>
    <input id="stored">
    <script>

            var coupons = ['hh'];
            var storedNames;
            localStorage.setItem("coupons", JSON.stringify(coupons));
            $('#button').on('click', function(){
                storedNames = JSON.parse(localStorage.getItem("coupons"));
                var enteredValue = $('#coupon').val();
                for(i=0; i <storedNames.length; i++) {
                    if(enteredValue === storedNames[i]) {
                        console.log("value Exist!!");
                        console.log(storedNames[]);
                    }
                    else {
                       console.log("in else");
                        coupons.push(enteredValue);
                        localStorage.setItem("coupons", JSON.stringify(coupons));
                        storedNames = JSON.parse(localStorage.getItem("coupons"));

                    }

                    }



            });



    </script>
</body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-01 06:14:12

如果存储的名称中不存在这些名称,您可能希望推送这些名称。请参阅所附代码。

代码语言:javascript
复制
    <html class=" reevoomark_ATY">
    <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Special Offers</title>
        <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="crossorigin="anonymous"></script>
    </head>
<body>
    <input id="coupon"> <button style="width:50px;padding:10px;background:grey;height:30px;border:1px solid grey" id="button">Apply</button>
    <input id="stored">
    <script>

            var coupons = ['hh'];
            localStorage.setItem("coupons", JSON.stringify(coupons));
            $('#button').on('click', function(){
                var storedNames = JSON.parse(localStorage.getItem("coupons"));
                var enteredValue = $('#coupon').val();
                if (storedNames.includes(enteredValue)) {
                    console.log("value Exist!!");
                  console.log(storedNames);
                } else {
                    console.log("in else");
                  storedNames.push(enteredValue);
                  localStorage.setItem("coupons", JSON.stringify(storedNames));
                }
            });



    </script>
</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51118792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档