フォームに入力した内容(まだ送信されていないページ)をブラウザに保存させて、入力したデータに影響を与えないようにするにはどうすればよいですか?提出されていないフォームデータを覚えておくには?
私はユーザーが番号を入力するフォームを持っています。初期状態では、フォームのデフォルトは0です。私はlocalStorageにデータを格納しているので、ブラウザはデータを覚えることができます。ただし、ページをリフレッシュすると、ユーザーが入力したデータは消え、デフォルトでは0が表示されます。 (まだのlocalStorageデータがそれのために存在する)
私はjQueryの
$(".formClassName").val(localStorage.getItem(key));
を使用しようとしましたが、それは動作しません。誰も私にこのことについて助言を与えることができますか?事前にありがとう。
編集:私はそれが問題に機能液を示してい
//JavaScript
<script>
//Using on because the website retrieves the above form dynamically
$(document).on("click", ".saveData", function(e){
//retrieve the number entered in the form
var userNum = $(this).siblings(".dataEntered").val();
//retrieve the value in name attribute
var thisFormName = $(this).attr("name");
//store the data
localStorage.setItem(thisFormName, userNum);
//Now that the save button has been pressed (not submitted to the
//server yet), and the data is stored in localStorage, I want to
//the page to show the number in userNum even after you refresh the page
//but this does not work.
$(".dataEntered").val(localStorage.setItem(thisFormName));
});
</script>
いつ、どのようにlocalStorageに値を格納していますか? – FabioG
私は質問にコードを追加させてください。 –
あなたはこれを実行していますか? '$("。formClassName ").Val(localStorage.getItem(key));' DOM準備完了? – Developer