【问题标题】:How can I store a selected radio button's text in localStorage?如何将选定单选按钮的文本存储在 localStorage 中?
【发布时间】:2013-03-27 21:26:07
【问题描述】:

谁能帮助我如何在 localStorage 中设置单选按钮选定的文本?这是我的代码,但我每次都得到未定义的值:

<label for="gender">GENDER:  
<input type="radio" id="radiobtn1" name="gender" value="0"/>  
<label for="radiobtn1">MALE</label>  
<input type="radio" id="radiobtn1" name="gender" value="1"/>
<label for="radiobtn1">FEMALE</label>  
</label> 
<br>

jQuery 代码;尝试编写这样的代码时,我得到了选定的值:

var tempgender = $("input:checked + label").text(); 
console.log("gender selcte value:" + tempgender);

但是当尝试通过编写此代码将选定的值保存在本地存储中时,然后得到“未定义的值”。

window.localStorage.setItem("tempgender",$("input:checked + label").text());`                                                  

我做错了什么?

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

代码是正确的,但我认为您的浏览器不支持 localstorage,请在使用 localstorage 之前执行

if(typeof(Storage)!=="undefined")
{
  alert("storage is supported and you can store data in it");
}
else
{
 alert("Get new version of browser or use browser which support storage");
}

更多详情,您可以查看:Store data locally with HTML5 Web Storage

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2015-06-27
    • 2019-12-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多