【问题标题】:How to update values in local storage dynamically,on button click?如何在单击按钮时动态更新本地存储中的值?
【发布时间】:2016-07-04 17:43:46
【问题描述】:

这是我用于向本地存储添加值的代码。

    var textval=0;
function addValue(){
    console.log(typeof(localStorage.getItem(textval)));        
    textval= localStorage.length+1;
    var fieldValue = $('#textfield').val(); 
    localStorage.setItem(textval, fieldValue);
    textval = 0;
}

现在,如果我想编辑本地存储中的值,该怎么办?

My HTML code used for updation lies below:-
<input class="form-control" id="ex2" type="text" placeholder="Enter the key"><br>
<input class="form-control" id="ex21" type="text" placeholder="Enter the updated value"><br>
<button type="button" class="btn btn-primary"    onClick="editValue();">Enter</button>

当我在文本框中指定键和更新值后单击 Enter 按钮时,这些更改应反映在我的本地存储中。我需要做什么? 请帮帮我。谢谢。..

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

尝试使用 select 和下面的 editValue 函数

<div class="parent">
<select class="form-control" id="ex2" name="key"></select><br>
<input class="form-control" id="ex21" type="text" placeholder="Enter the updated value"><br>
<button type="button" class="btn btn-primary"    onClick="editValue();">Enter</button>
</div>

js:

 for (var i = 0; i < localStorage.length; i++) {
    $('#ex2').append('<option value="'+localStorage.key(i)+'">'+localStorage.key(i)+'</option>');
  };

function editValue() {
   var key = $(this).closest('.parent').find('option:selected').val();
  localStorage.setItem(key,$(this).closest('.parent').find('input[type="text"]').val());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多