localStorage how to save a checkbox

后端 未结 4 1307
傲寒
傲寒 2020-12-09 12:49

I want to save a checkbox with localstorage. So that when i have checked the box and I close the browser and i re-open it, it will still be checked. right now if i click on

4条回答
  •  执念已碎
    2020-12-09 13:10

    The problem is that you are storing value as "true" in localStorage which is a string format, Now at time of loading the page value is retrieved as string and you are comparing that String "true" with boolean true. This will return false. One small change as

    if (checked == "true")
    

    now this should work.

提交回复
热议问题