2016-07-26 20 views
2

我有一個默認選中的頁面中的複選框。我嘗試使用LocalStorage並在用戶更改時將值保存在那裏。複選框上的Jquery道具在頁面加載時不起作用

這是我做了什麼:

<script language="javascript" type="text/javascript"> 
     jQuery(document).ready(function ($) { 

      $('#togglelist').prop('checked', localStorage.ListCheckbox); 

      $("#togglelist").change(function() { 
       localStorage.ListCheckbox = $('#togglelist').prop('checked'); 
      }); 

    </script> 

我可以在開發者控制檯中看到價值,同時在複選框點擊改變。此外,如果我將其保留爲未選中狀態(false),則在下一頁加載時,本地存儲的值爲false,但複選框仍處於檢查狀態。

編輯:我不能使用checkboxradio('refresh')因爲我沒有JQM。

+0

'$( '#togglelist')。道具的價值形式localStorage總是存儲數據('checked',localStorage.ListCheckbox ==='true');' – Rayon

+0

Omg。就是這個。你能否把它添加爲答案? – Tasos

+1

由於'非空'字符串總是真的,'localStorage'總是以字符串形式存儲數據。 – Rayon

回答

4

由於non-empty字符串總是truthy和string

測試localStoragestring

$('#togglelist').prop('checked', localStorage.ListCheckbox==='true'); 
+1

在9分鐘內接受答案(由於SE的限制) – Tasos

相關問題