2012-03-01 27 views
0

當我點擊複選框時,應該啓用它,直到我點擊複選框,即使應用程序關閉或我在另一個html Page.Once複選框被再次單擊時,它應該被禁用。我應該如何啓用使用jquery或javascript的複選框?

+0

你的意思是你想存儲頁面之間的複選框狀態?你可以使用phonegap的存儲API將它寫入某些東西,然後加載該值並在下次加載該頁面時更新複選框? – Rup 2012-03-01 13:24:36

+3

爲什麼不設置啓用/禁用的cookie? – Bakudan 2012-03-01 13:31:06

回答

0

另一種選擇是HTML 5本地存儲。我相信這是在IE 8+和所有其他主流瀏覽器中支持的。這裏是你會怎麼做一個例子:

//Runs once the DOM is ready 
$(function() { 
    //Store the check box element in a variable 
    var checkbox = $("#elementID"); 
    //The change event gets triggered every time the check box element is clicked 
    checkbox.change(function() { 
     //Set the checkbox value in local storage 
     localStorage.setItem("checkboxValue", checkbox.prop("checked")); 
    }); 
    var checkValue = localStorage.getItem("checkboxValue"); 
    //Since localStorage returns a string, you must catch it 
    //and then check or uncheck the box 
    if(checkValue === "true") { 
     //Check the box 
     checkbox.prop("checked", true) 
    } 
    else { 
     //Uncheck the box 
     checkbox.prop("checked", false) 
    } 
}); 
+0

請記住,這隻適用於jQuery版本> 1.6。如果您使用的是jQuery <1.6版本,請將「prop」方法替換爲「attr」 – 2012-03-01 18:24:44