简体   繁体   English

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

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

Can anyone help me how to set the radio button selected text in localStorage? 谁能帮我在localStorage中设置单选按钮所选文本? Here is my code but I'm getting undefined value at every time: 这是我的代码,但是每次都得到未定义的值:

<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 code; jQuery代码; I'm getting selected value when trying to write the code like this: 尝试编写如下代码时,我得到选择的值:

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

But when trying to save selected value in localstorage by writing this code then getting "undefined value". 但是,当尝试通过编写此代码将选定的值保存在本地存储中,然后获得“未定义的值”时。

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

What am I doing wrong? 我究竟做错了什么?

code is correct but i think localstorage is not supported by your browser , do perform before using localstorage 代码是正确的,但我认为您的浏览器不支持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");
}

for further details you can check : Store data locally with HTML5 Web Storage 有关更多详细信息,请检查: 使用HTML5 Web存储在本地存储数据

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

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