Php 仅在提交时使用javascript保存表单状态
所以。我有一张有很多复选框的表格。除此之外,我还有一段javascript代码,当用户按下submit时,它应该保存每个复选框的状态。我的问题是两件事。 问题:我只想在提交表单时将复选框状态保存到cookie,现在如果我标记复选框并重新加载页面而不提交表单,则会保存复选框状态。我正在使用Javascript和Cookie,这两件事我还很陌生。所以我非常感谢大家的帮助。这是我的代码,我是从:Php 仅在提交时使用javascript保存表单状态,php,javascript,jquery,html,forms,Php,Javascript,Jquery,Html,Forms,所以。我有一张有很多复选框的表格。除此之外,我还有一段javascript代码,当用户按下submit时,它应该保存每个复选框的状态。我的问题是两件事。 问题:我只想在提交表单时将复选框状态保存到cookie,现在如果我标记复选框并重新加载页面而不提交表单,则会保存复选框状态。我正在使用Javascript和Cookie,这两件事我还很陌生。所以我非常感谢大家的帮助。这是我的代码,我是从: 因此,我只想在提交时保存到cookie。绑定到表单的submit事件,而不是所有复选框的change事件
因此,我只想在提交时保存到cookie。绑定到表单的
submit
事件,而不是所有复选框的change
事件
请尝试使用此函数代替第二个函数:
jQuery(function($) {
// bind to the submit event of the form
$('#id-of-your-form').submit(function() {
// get storage
var storedData = getStorage('com_mysite_checkboxes_');
// save checkbox states to cookie
$('div.check input:checkbox').each(function() {
// for each checkbox, save the state in storage with this.id as the key
storedData.set(this.id, $(this).is(':checked')?'checked':'not');
});
});
});
jQuery(document).ready(function() {
// on load, restore the checked checkboxes
$('div.check input:checkbox').each(function() {
// get storage
var storedData = getStorage('com_mysite_checkboxes_');
// for each checkbox, load the state and check it if state is "checked"
var state = storedData.get(this.id);
if (state == 'checked') {
$(this).attr('checked', 'checked');
}
});
});
绑定到表单的
submit
事件,而不是所有复选框的change
事件
请尝试使用此函数代替第二个函数:
jQuery(function($) {
// bind to the submit event of the form
$('#id-of-your-form').submit(function() {
// get storage
var storedData = getStorage('com_mysite_checkboxes_');
// save checkbox states to cookie
$('div.check input:checkbox').each(function() {
// for each checkbox, save the state in storage with this.id as the key
storedData.set(this.id, $(this).is(':checked')?'checked':'not');
});
});
});
jQuery(document).ready(function() {
// on load, restore the checked checkboxes
$('div.check input:checkbox').each(function() {
// get storage
var storedData = getStorage('com_mysite_checkboxes_');
// for each checkbox, load the state and check it if state is "checked"
var state = storedData.get(this.id);
if (state == 'checked') {
$(this).attr('checked', 'checked');
}
});
});
这是很多代码,你已经告诉我们很多你想做的事情。您遇到了什么问题?也许您应该看看jQuery。我试图缩短代码并添加了一个更精确的问题。问题是:$('div.check-input:checkbox').bind(“更改”您实际上是告诉它在复选框的更改事件时保存这是一大堆代码,并且您告诉了我们许多您想做的事情。您遇到了什么问题?也许您应该看看jQuery。我试图缩短代码并添加了一个更精确的问题。问题是:$(“div.check-input:checkbox”).bind('change')您实际上是告诉它在checkboxes的更改事件时保存这是一件事,听起来像我在寻找的东西,但在提交和重新加载后,状态不再保存。或者至少没有被检索。因此,中途:)@Rockstaid,我已经编辑了我的答案来恢复复选框的状态。这是一件事,听起来像是我在寻找的,但是在提交和重新加载后,状态不再保存。或者至少没有被检索。因此,在这中途:)@Rockstaid,我已经编辑了我的答案来恢复复选框的状态。