清除后退按钮上的表单?

发布于 2024-12-08 01:32:12 字数 242 浏览 2 评论 0原文

我有一个表单中包含多个复选框的页面,当选中这些复选框时,会使用 jquery 从表中过滤出相应的信息。我的问题是以下场景:

  1. 有人选中一个或多个框,隐藏表中的行

  2. 他们单击链接,转到另一页

    p>
  3. 他们单击后退按钮。

此时,复选框仍处于选中状态(至少在 Chrome 中),但所有表格行再次可见。

有什么想法吗?

I have a page with several check-boxes in a form, which when selected, filter corresponding info out of a table with jquery. My problem is the following scenario:

  1. Someone checks one or more boxes, hiding rows from table

  2. They Click a link, going to another page

  3. They click the back button.

At this point, the check boxes are still checked (in chrome at least), but the all the table rows are visable again.

Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰ゝ天使的微笑 2024-12-15 01:32:12

如果您的目标是让他们回到离开前的确切视图(表格行仍然隐藏):

这是网站的缺点(或优点):它是无状态的,特别是当它涉及客户端操作。

您需要做的是将应用程序状态存储在 URL 哈希标记中,以便当用户使用后退按钮时,您可以检索“状态”并重建其视图。

这是另一个问题,其答案可能会对您有所帮助: jquery javascript:添加浏览器历史记录带有主题标签?

如果您的目标是将页面返回到其初始状态:

只需重置页面加载时的所有复选框:

$('input:checkbox').prop('checked', false);

-或-

重置整个表单:

$(':input','#myform')
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .prop('checked', false)
 .prop('selected', false);

If your objective is to bring them back to the exact view they had before they left (table rows are still hidden):

Such is a disadvantage (or advantage) of a website: it's stateless, particularly when it comes to client-side manipulation.

What you'll need to do is store the application state in the URL hash tag so that when the user uses their back button, you can retrieve the "state" and reconstruct their view.

Here's another question whose answer might help you: jquery javascript: adding browser history back with hashtag?

If your objective is to return the page back to its initial state:

Just reset all the checkboxes on page load:

$('input:checkbox').prop('checked', false);

-or-

Reset the entire form:

$(':input','#myform')
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .prop('checked', false)
 .prop('selected', false);
熟人话多 2024-12-15 01:32:12

不允许缓存您的页面,或添加代码来重置表单:

$("form input").each(function(){
    var elem = $(this);
    var type = elem.attr("type");
    if(type == "checkbox") elem.prop("checked", "");
    else if(type == "text") elem.val("");
});

必须使用 .each,因为 CSS 选择器 input[type=text] 不会这样做t 匹配 ,而该元素仍然是文本元素。

Do not allow your page to be cached, or add code to reset your form:

$("form input").each(function(){
    var elem = $(this);
    var type = elem.attr("type");
    if(type == "checkbox") elem.prop("checked", "");
    else if(type == "text") elem.val("");
});

.each has to be used, because the CSS selector input[type=text] doesn't match <input />, while the element is still a text element.

梦魇绽荼蘼 2024-12-15 01:32:12

对于 IE 在提交后重置表单,对我没有帮助。相反,以下代码对我有用。页面加载事件也会在浏览器后退按钮上调用。

$(window).load(function() {
    $('form').get(0).reset(); //clear form data on page load
});

For IE resetting the form after submit, did not help for me. Instead following code worked for me. Page load event gets called on browser Back Button as well.

$(window).load(function() {
    $('form').get(0).reset(); //clear form data on page load
});
对你而言 2024-12-15 01:32:12

因此,您的问题不在于浏览器缓存用户输入,而在于行未被隐藏。您可以通过添加一个 jQuery 脚本来轻松解决此问题,该脚本将检查复选框是否被选中(加载页面后)。如果选中,则隐藏该行:)

伪代码:

1. wait for the page to load
2. for each checkbox check if checked
3. if it's checked, hide the corresponding row in the same way you did when the user checked it.

So your problem is not with the browser caching the user input but with the rows not being hidden. You can easily solve this problem by adding a jQuery script that will check if a checkbox is checked (after loading the page). If it's checked, you hide the row :)

pseudocode:

1. wait for the page to load
2. for each checkbox check if checked
3. if it's checked, hide the corresponding row in the same way you did when the user checked it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文