HTML5会话存储列表

时间:2015-09-18 05:46:58

标签: javascript html5 session-storage

如何为下拉列表定义sessionStorage?我有以下内容,但我得到了#34; someTitle未定义"

不确定我做错了什么。

的Javascript

function save_page(form) {
    if (supports_html5_storage()) {
        var title = $('.btn-group-month ul li > a').attr('title');
        sessionStorage.setItem("someTitle", title);
    }
}

function load_page(form) {
    if (supports_html5_storage()) {
        var title = sessionStorage.getItem("someTitle");
        $('.btn-group-month ul li > a').attr('title', title);
    }
}

HTML

<div class="btn-group btn-group-month">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span data-bind="label">Month</span>
  </button>
  <ul class="dropdown-menu dropdown-menu-month">
    <li><a href="#" title="January">January</a></li>
    <li><a href="#" title="February">February</a></li>
    <li><a href="#" title="March">March</a></li>
  </ul>
</div>

fiddle

1 个答案:

答案 0 :(得分:1)

在代码中,您正在读取不正确的数据。这样:

var title = $('.btn-group-month ul li > a').attr('title');

将读取列表中第一个链接的title属性。但那不是你想要的价值。该值将始终为“1月”,它将损坏列表其余部分的值(由评论中的metal03326指定)。

您想要阅读(和设置)的内容是按钮中第一个span的值,因为这是选定的值。为此,您需要稍微更改选择器:

var title = $('.btn-group-month button > span:first()').text();

这将正确读取所选值。现在要在加载新页面时再次设置它,您还需要修改span

var title = sessionStorage.getItem("someTitle");
if (title) {
    $('.btn-group-month button > span:first()').text(title);
}

您可以在此处查看整个代码,但它不起作用,因为它在沙盒模式下不支持sessionStorage。所以you can see it working on this JSFiddle too

function supports_html5_storage() {
  try {
    return 'sessionStorage' in window && window['sessionStorage'] !== null;
  } catch (e) {
    return false;
  }
}

function save_page(form) {
  if (supports_html5_storage()) {

    var storage = window.sessionStorage;

    // AM - new selector
    var title = $('.btn-group-month button > span:first()').text();
    sessionStorage.setItem("someTitle", title);

    form.filter(':input').each(function(ind,elem) {
      if (elem.name && elem.value) {
        storage.setItem(elem.name,elem.value);
      }
      else {
        //storage.someTitle=$('ul.dropdown-menu-month li').find('a').attr('title');
      }
    });
  } else {
    alert("HTML5 storage not available");
  }
}

function load_page(form)
{
  if (supports_html5_storage()) {

    var title = sessionStorage.getItem("someTitle");
    // AM - new selector
    if (title) {
      $('.btn-group-month button > span:first()').text(title);
    }

    var storage = window.sessionStorage;

    form.filter(':input').each(function(ind,elem) {
      if (elem.name) {
        $('.edit-image-container img.img-verify').attr('src',storage.galleryImg);
        elem.value = storage.getItem(elem.name);
      } else {
        //
      }
    });
  }
}


// Enable dropdown to display chosen value
$('.dropdown-menu li').click(function(){
  var elementVal = $(this).text();

  $(this).closest('.input-append').find('#appendedInputButton').val(elementVal);
});


// Dropdown picker
$('.dropdown-menu').on( 'click', 'li', function( event ) {
  var $target = $( event.currentTarget );
  $target.closest( '.btn-group' )
  .find( '[data-bind="label"]' ).text( $target.text() )
  .end()
  .children( '.dropdown-toggle' ).dropdown( 'toggle' );

  // AM - Save form
  save_page($("form"));

  return false;
});

// AM - Load form
load_page($("form"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<form>
  <div class="btn-group btn-group-month">
    <button type="button" name="dropdownval" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
      <span data-bind="label">Month</span><span class="caret"></span>
    </button>
    <ul class="dropdown-menu dropdown-menu-month" name="month">
      <li><a href="#" title="January">January</a></li>
      <li><a href="#" title="February">February</a></li>
      <li><a href="#" title="March">March</a></li>
    </ul>
  </div>
</form>