繁体   English   中英

如何将所选单选按钮的文本存储在localStorage中?

[英]How can I store a selected radio button's text in localStorage?

谁能帮我在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());`                                                  

我究竟做错了什么?

代码是正确的,但我认为您的浏览器不支持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");
}

有关更多详细信息,请检查: 使用HTML5 Web存储在本地存储数据

暂无
暂无

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

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