我有一个输入,在下面显示本地存储中的数据。此刻,它当前不断添加已输入的内容,并在两者之间显示“。”。很好,但是我所需要的只是存储和显示一个输入并显示它。如果用户想更改输入,将有一个清除本地存储的按钮。
任何人都可以为此功能提供帮助。
<script>
function storeText(inputID) {
//check to see if the localStorage item already exists
var userInput = localStorage.userInfo;
//set it up for new data to be appended
if(userInput!=null) userInput+=" ";
else userInput="";
//add the new data
userInput += document.getElementById(inputID).value;
//set the localStorage field with the updated data
localStorage.userInfo = userInput;
//write it to the page
document.getElementById("result").innerHTML = localStorage.userInfo;
if (userInput > 1){
alert("Please Clear to add a new goal");
return false;
}
</script>
这是我的输入和显示
<input id="userText" type="text"/>
<input type="button" value="store" onclick="storeText('userText')"/>
<div id="result">
Result here
</div>
localStorage.clear(); -这将完全清除本地存储
localStorage.removeItem('userInput'); -这会删除特定的项目-然后就可以重新添加它。
只需将其应用于您的按钮onClick事件
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句