0

按照这里的教程,http://css-tricks.com/video-screencasts/96-localstorage-for-forms/。我能够使用提交按钮重新创建功能,但是,是否可以使用复选框而不是按钮?

这是我到目前为止所拥有的,

if (localStorage) {

$(function() {

    // Insert new buttons
    $("#savelinks")
        .after("<input type='submit' value='Save Address' id='saveData'> &nbsp;&nbsp;")
        .after("<input type='submit' value='Clear Saved Address' id='clearData'>");

    $("#saveData")
        .click(function(e) {

                    e.preventDefault();
                    // Test if saved data exists on page load
                    localStorage.setItem("flag", "set");
                    var data = $("#form1").serializeArray();

                    // iterate over results
                    $.each(data, function(i, obj) {

                                   localStorage.setItem(obj.name, obj.value);

                     });

        });



    if (localStorage.getItem("flag") == "set") {
        var data = $("#form1").serializeArray();
        $.each(data, function(i, obj) {
            $("[name='" + obj.name +"']").val(localStorage.getItem(obj.name));
        });
    }   



    $("#clearData")
        .click(function(e) {
            e.preventDefault();
            localStorage.setItem("flag", "");
        });
});
}

先感谢您!

4

1 回答 1

0

使用change事件,查看复选框是否被选中;

$('#MyCheckbox').change(function() {

  var $this = $(this), // Current checkbox
      isChecked = this.checked, // We can also use $this.prop('checked')
      data = $('#form1').serializeArray();

  if (isChecked) {
    // Store serialized form data
  } else {
    // Clear stored form data
  }

});
于 2013-03-04T18:29:55.953 回答