Clear local storage for div on radio selection

2019-09-18 13:39发布

I'm trying to get local storage to clear for all fields in one specific div when the user selects the No radio button:

Slightly incomplete jsfiddle but should do the trick

I'm obviously not doing something right here (.hide-show-yes is where I want all fields inside that div to clear):

$(document).ready(function(){
$('input[name="for_person"]').on('click', function () {
    if ($('#personNo').prop('checked')) {
    // ??
        $('.hide-show-yes').localStorage.removeItem('fname');

    } else {

        // do nothing

    }
});
});

1条回答
走好不送
2楼-- · 2019-09-18 14:13

localStorage is not a jQuery method . Also

$(document).ready([function() {

    if (localStorage["dontclear"]) {
      $('#dontclear').val(localStorage["dontclear"]);
    }
    if (localStorage["fname"]) {
      $('#fname').val(localStorage["fname"]);
    }
    if (localStorage["lname"]) {
      $('#lname').val(localStorage["lname"]);
    }
  console.log(localStorage)
}, function() {
  $('.stored').change(function() {
    localStorage[$(this).attr('name')] = $(this).val();
    console.log(localStorage)
  });

  $('input[name="for_person"]').on('click', function() {
    if ($('#personNo').prop('checked')) {
      // ??
      localStorage.removeItem('fname');

    } else {

      // do nothing

    }
  });
}]);

plnkr http://plnkr.co/edit/LPw3zbpgrhJkSh1hdKXG?p=info

查看更多
登录 后发表回答