1

表单的 Javascript 重置方法不应该影响隐藏输入,但在 IE8 和 IE7 中它无论如何都会影响。有谁知道使它正常运行的方法?

我知道我可以通过循环单个输入或在重置表单然后恢复它们之前记住值来做到这一点,但我想知道是否有人能想出一个更简单的方法。如果它能让您的解决方案更简单,请随意使用 jQuery。你可以在http://jsfiddle.net/Ddtz4/看到一个例子。

根据插件作者的说法,这应该不是 IE 错误,reset 方法对没有初始值的元素的影响是未定义的。

4

4 回答 4

0

You could use a JavaScript function to capture the reset button push, save those values as cookies, then return true to complete the reset process. On reload, you can check for those cookies and plug those values back in.

于 2012-05-03T23:12:34.637 回答
0

我正在考虑这样的事情,使用jQuery:

$(document).delegate("form", "resetForm", function() {
    $(this).find("input[type=hidden]").each(function() {
        $(this).prop("defaultValue", $(this).val());
    });
    this.reset();
});

每当我想重置表单时,我都可以这样做:

$("#whateverForm").trigger("resetForm");
于 2012-05-03T23:46:33.473 回答
0

以下片段似乎也适用于 IE8

$("#testInput").val('foo');
$("#testForm")[0].reset();
$("#result").html($("#testInput").val());

更新:

你为什么不创建一个自定义的重置函数()来代替:

$("#resetbutton").on("click", function() {
    var form = $(this).closest('form');
    form.find("input:not(:hidden)").val('');
    form.find("select").prop('selected', false);
    // so on

});
于 2012-05-03T23:56:48.793 回答
0

一种简单的方法是reset()在重置时暂时从表单中删除隐藏元素

function fixReset() {
    var testForm =  document.getElementById("testForm");
    testForm._nativeReset = testForm.reset;
    testForm.reset = function(){
        var hidden = $('input[type=hidden]',this).detach();
        this._nativeReset();
        $(this).append(hidden);
    }
};
$(document).ready(fixReset);

这样,如果您愿意,您仍然可以使用本机重置按钮。这种方法可以很容易地扩展到处理所有表格

于 2012-05-05T02:42:46.230 回答