1
テキストエリアに入力したメールをlocalStorage
に保存しようとしています。私は、ページを読み込むときにプレースホルダ "あなたのGmailのアドレス"が表示されますが、値を入力して「保存」をクリックすると何も起こりません。テキストが保存されていることを確認するアラートまたはコンソールは表示されません。私は間違って何をしていますか?textareaからlocalStorageに保存する方法は?
<html>
<head><title>Extension Options</title></head>
<body>
<textarea id="user" style="margin-bottom: 4px; width: 250px; height: 20px">
</textarea><br />
<button id="save">Save</button>
<!--<button id="save">Clear</button>-->
<script type="text/javascript">
//check if "user" is in localStorage
if (localStorage["user"])
{
var user = localStorage["user"] ;
document.getElementById("user").value = user ;
alert("you are already a user")
}
else
{
document.getElementById("user").placeholder = "your gmail address" ;
console.log("user no found in localStorage")
}
//save entered gmail address
document.getElementById("save").addEventListener
{
"click", function()
{
var user = document.getElementById("user").value ;
//localStorage["user"] = user ;
localStorage.setItem("user", user) ;
alert("gmail id saved") ;
console.log("gmail id saved")
} , false
}
</script>
</body>
</html>
:それはこのように使用する必要があります。 – pimvdb
@pimvdb、はい、そうです、中括弧でもう一度試してみましたが、コンソールにエラーメッセージが表示されました。 – Zeynel