localStorage,选择菜单中的选项数据在更改页面时消失

时间:2015-06-11 14:03:36

标签: javascript jquery html cordova

我有这个代码将输入的电话号码写入选择菜单的已定义选项,问题是当我返回或退出应用程序时数据被删除且列表为空。我无法找出原因以及如何解决这个问题......

    var flag_beneficiary_account_number=false;
     var beneficiary_account_number;
if ( $('#beneficiary_number').val().length > 2 ) {
    beneficiary_account_number=$('#beneficiary_number').val(); 
    flag_beneficiary_account_number=true;
} else {
    beneficiary_account_number = $('#Beneficiary_number_select').find(":selected").html();
    flag_beneficiary_account_number=false;
}

if(flag_beneficiary_account_number){

    if(localStorage.getItem("Beneficiary_number_select1")!=beneficiary_account_number &&
        localStorage.getItem("Beneficiary_number_select2")!=beneficiary_account_number &&
        localStorage.getItem("Beneficiary_number_select3")!=beneficiary_account_number){

   var select_counter = parseInt(localStorage.getItem("select-counter"));
        var select_option = select_counter%3;
        select_counter = select_counter+1;
        localStorage.setItem("select-counter",select_counter);

        switch  (select_option){
            case 0:
                localStorage.setItem("Beneficiary_number_select1",beneficiary_account_number);
                $('#Beneficiary_number_select1').val(beneficiary_account_number);
                $('#Beneficiary_number_select1').html(beneficiary_account_number);
                break;
            case 1:
                localStorage.setItem("Beneficiary_number_select2",beneficiary_account_number);
                $('#Beneficiary_number_select2').val(beneficiary_account_number);
                $('#Beneficiary_number_select2').html(beneficiary_account_number);
                break;
            case 2:
                localStorage.setItem("Beneficiary_number_select3",beneficiary_account_number);
                $('#Beneficiary_number_select3').val(beneficiary_account_number);
                $('#Beneficiary_number_select3').html(beneficiary_account_number);
                break;

            default:
                break;


        }
    }
}

1 个答案:

答案 0 :(得分:1)

当您最终添加cordova标记时,我正在编辑您的问题。事情变得清晰,所以你需要记住在cordova,让localStorage工作,以这种方式使用

   // To store
   window.localStorage.setItem("Beneficiary_number_select2", beneficiary_account_number);

   //and to access the value, use
  window.localStorage.localStorage
      .getItem("Beneficiary_number_select2");

代替localStorage.getItem("Beneficiary_number_select2")