简体   繁体   English

如何在本地存储中从文本框和下拉列表中保存值?

[英]How to save the values from a textbox and dropdown in localStorage?

I want to save the values entered in a textbox and also the one selected from a drop-down into localstorage. 我想保存在文本框中输入的值,也要保存从下拉列表中选择的值到本地存储。 I tried the following code: 我尝试了以下代码:

<table class="dynatable" id="my">
    <tr>
        <td><input type="text" id="name" name="name1" onchange="saveSelectedValues();"></td>
        <td><select id="type" onchange="saveSelectedValues();"><option>Text</option><option>Paragraph</option><option>Dropdown</option></select>
    </tr>
    <tr>
        <td><input type="button" id="add" value="Add" onclick="Javascript:addRow()"></td>
    </tr>
</table>

and my saveSelectedValues function is as below: 我的saveSelectedValues函数如下:

function saveSelectedValues(){
    var myName = document.getElementById("name").value;
    var type = document.getElementById("type").value;

    localStorage.setItem("attributeName",myName);
    localStorage.setItem("attributeType",type); 
}

But the values are not getting saved in localstorage. 但是这些值不会保存在本地存储中。

Your code works fine 您的代码工作正常

saveSelectedValues = function(){
    var myName = document.getElementById("name").value;
    var type = document.getElementById("type").value;

    localStorage.setItem("attributeName",myName);
    localStorage.setItem("attributeType",type); 
     alert(localStorage.getItem("attributeName"));
}

look for below jsfiddle 在下面寻找jsfiddle

jsfiddle jsfiddle

Make sure your browser is supporting html 5 local storage functionality and setting to allowed local storage. 确保您的浏览器支持html 5本地存储功能并将其设置为允许的本地存储。

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

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