简体   繁体   English

我在使用localStorage存储输入值时遇到一个烦人的bug

[英]I got a annoying bug in storing input value using localStorage

I want to do is to store the current inputs even if the user refresh or close the browser. 我要做的是即使用户刷新或关闭浏览器也要存储当前输入。

My problem is if i click Yes in the radio button and refresh the page or close the browser and reopen it the No button is checked and the Yes button is unchecked. 我的问题是,如果我点击Yes在单选按钮,并刷新页面或关闭浏览器,然后重新打开它的No按钮被选中并且Yes按钮没有被选中。

testing link: http://jsfiddle.net/5kcsn/124/ 测试链接: http : //jsfiddle.net/5kcsn/124/

current script: 当前脚本:

$(document).ready(function () {

    $('#employed_v1, #employed_v0, #test').on("change", function () {
       debugger;
        localStorage.setItem($(this).attr("id"), $(this).val())
    });


    $('#employed_v1, #employed_v0, #test').each(function (ind, val) {
            debugger;
                if ($(val).attr("id") != "employed_v1") {
                    $(val).val(localStorage.getItem($(val).attr("id")))
                }
                if ($(val).attr("id") != "employed_v0") {
                    $(val).val(localStorage.getItem($(val).attr("id")))
                }
                else {
                    if (localStorage.getItem($(val).attr("id")) == "Yes"){
                        $("#employed_v1[value=Yes]").prop("checked", true);}
                    if (localStorage.getItem($(val).attr("id")) == "No"){
                        $("#employed_v0[value=No]").prop("checked", true);} 

                }

            });



$('[id="employed_v0"]').on('click', function(){
        $('#test').val('');
        $('#test').prop('disabled', true);
     });
$('[id="employed_v1"]').on('click', function(){
        $('#test').prop('disabled', false);
     });

});

To store the check state you should use the checked attribute. 要存储检查状态,您应该使用checked属性。 However, here is a modified version : 但是,这是修改后的版本:

$(document).ready(function () {

    $('#employed_v1, #employed_v0').on("change", function () {
        localStorage.setItem('employed', $(this).attr("id"))
    });

    $('#' + localStorage.getItem('employed')).attr('checked', true);
});

There are many things to consider here. 这里有很多事情要考虑。 First, the change-event is only fired on the one radiobutton that is clicked, and you are storing the value of the radiobutton to localstorage. 首先,仅在单击的一个单选按钮上触发更改事件,并且您将单选按钮的值存储到本地存储。 But that information is the same that is already present in the HTML, so it really doesn't serve the purpose. 但是该信息与HTML中已经存在的信息相同,因此它实际上没有达到目的。 The radiobuttons are grouped by the name-attribute, so you could store only one variable that tells which value of the radiobuttons is the selected one, here is an example: 单选按钮按名称-属性分组,因此您只能存储一个变量,该变量告诉单选按钮的哪个值是所选的,这是一个示例:

Html: HTML:

<input type="radio" name="employed" id="employed_v1" value="Yes" required="required" class="js-store" />Yes
<br />
<input type="radio" name="employed" id="employed_v0" value="No" required="required" class="js-store" />No
<br>
<input type="text" name="test" id="test" class="js-store" />

Javascript: Javascript:

  <script type="text/javascript">
    $(document).ready(function () {
        $('.js-store').on("change", function () {           
            if ($(this).is(":radio")) {                 
                localStorage.setItem($(this).attr("name"), $(this).val());
            }
            else {
                localStorage.setItem($(this).attr("id"), $(this).val());    
            }
        });

        $(".js-store").each(function() {                
            if ($(this).is(":radio")) {
                var value = localStorage.getItem($(this).attr("name"));                 
                if (value) { $(this).prop("checked", value === $(this).val()); };
            }
            else {
                var value = localStorage.getItem($(this).attr("id"));
                if (value) {$(this).val(value);}
            }               
        });
    });
</script>

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

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