简体   繁体   English

可以在sessionStorage中使用jquery吗?

[英]Can you use jquery in sessionStorage?

I have code like this: 我有这样的代码:

$(document).ready(function() {

var field = $('#field').val();

if (sessionStorage.getItem('save')) {
    $('#field').val(sessionStorage.getItem('save'));
}

field.addEventListener("change", function() {
    sessionStorage.setItem('save', field);
});

});

<input id="field" type="text"></input>

http://jsfiddle.net/oae8krpm/ http://jsfiddle.net/oae8krpm/

But it doesn't seem to work with jQuery, can it? 但这似乎不适用于jQuery,可以吗?

Thanks 谢谢

field.addEventListener

field is the text value of the field, not the DOM object for the field. field是该field的文本值,而不是该字段的DOM对象。 Use $('#field').get(0).addEventListener , or just jQuery event handling ( on et al). 使用$('#field').get(0).addEventListener ,或者只是jQuery的事件处理( on等)。

sessionStorage.setItem('save', field);

field is the text value of the field at the point you read it on document-ready. field是您在准备文档时阅读该字段时的文本值。 If you want to store the new value it was changed to, you'll need to read it again: 如果要存储更改为的新值,则需要再次读取它:

$('#field').on('change', function() {
    sessionStorage.setItem('save', $('#field').val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM