Storing checkbox value in local storage

前端 未结 3 1135
梦毁少年i
梦毁少年i 2021-01-07 00:28

I have a checkbox whose value $row[\'uid\'] I would like to store in local storage using javascript or jquery. When the user \"unchecks\" the checkbox, the valu

3条回答
  •  有刺的猬
    2021-01-07 00:53

    localStorage has two main functions, getItem and setItem. For setItem you pass in a key and a value. If you write to that key again, it will rewrite that value. So in your case, if a box is checked you would do localStorage.setItem("checkbox_value", true) and when it is unchecked you would pass in false instead. To get the value you can look at using jQuery like so: $(checkbox).is(':checked') and use a simple if-else clause to pass in true or false. then when you reload your page, on $( document ).ready() you can get the values using localStorage.getItem(key) and use Javascript to set the check boxes values.

    This is how i would go about using localStorage to remember check box values.

    Hope i helped! Ask me if anything is unclear.

提交回复
热议问题