【发布时间】:2015-09-01 12:55:12
【问题描述】:
我有一个 HTML from 来捕获字符串数据。当我通过ajax点击保存按钮时,我正在保存那些。但是范围还包括在检查值不为空后,一旦我使用橙色保存按钮将焦点移出表单字段,就将数据保存在会话存储中。这个想法是使用存储在会话存储中的值预先填充每个表单字段。一切正常,但我无法弄清楚会话存储部分。困难的部分是如何为每个表单字段分配一个唯一键,并使用该键在字段中查找和预加载值。
这是我的 JS
$('.wrapper').remove()
function renderInput() {
var inputField = '<div class="wrapper"><input class="zoom-text-field" type="text" value=""/><span class="close">save</span></div>';
return inputField;
}
function hideZoomFiled(e, textData) {
$(e).parent().parent().children('.students-name-collection').val(textData);
console.log(textData)
$(e).parent().hide();
$('.students-name-collection').attr('disabled', false);
$(e).prop('disabled', false);
}
function disableInputField(obj) {
$('.students-name-collection').attr('disabled', false);
$(obj).attr('disabled', true);
}
$('.students-name-collection').on('focus', function () {
disableInputField(this);
$(this).next('.wrapper').focus();
$('.wrapper').remove();
$(this).parent().append(renderInput());
var textData = '';
$('.close').on('click', function () {
textData = $(this).parent().children().val();
hideZoomFiled(this, textData);
});
$('.zoom-text-field').on('blur', function(){
if($(this).val() !== ''){
//save the value in the sessionstorage
//This is where I am getting lost
}
});
});
$('#submitForm').on('click', function(){
sessionStorage.clear();
})
// on page load read the session storage and pre fill the form fields
这是我的小提琴
【问题讨论】:
-
你查过how to use SessionStorage,并尝试过实现吗?
-
@SpencerWieczorek -- 是的 jsfiddle.net/sghoush1/ykshgrxg/6 我要空了。
-
该示例将该数据保存到会话存储中。 I've added a
console.log()to show that。输入一个值,点击“保存”,然后点击“运行”,你会发现数据就在那里。 -
@SpencerWieczorek -- 你能分享你在哪里做的链接吗?
-
@SpencerWieczorek -- 是的..这正是我迷路的地方。我刚刚用您的评论更新了问题
标签: javascript jquery