Javascript 表单重置无法使用 jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8937285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:08:29  来源:igfitidea点击:

Form Resetting is not working using jquery

javascriptjqueryajax

提问by Kichu

I want to reset the form after calling an ajax function. This is the code i gave in the jquery:

我想在调用 ajax 函数后重置表单。这是我在 jquery 中给出的代码:

$("#frm_silder").reset();

Here frm_silderis the id of form. But when I'm using this code i got an eorror message like this.

frm_silder是表单的 id。但是当我使用此代码时,我收到了这样的错误消息。

$("#frm_silder").reset is not a function

In my html i give the id to form like this:

在我的 html 中,我将 id 设置为如下形式:

<form name="frm_silder" id="frm_silder" method="post">

So what is the problem in my code?

那么我的代码有什么问题呢?

回答by Sameera Thilakasiri

In jQuery

在 jQuery 中

$('#frm_silder')[0].reset();

in Javascript

在 JavaScript 中

document.getElementById('frm_silder').reset()

回答by xbonez

You need to reset each element individually. Jquery does not have a function reset()that works on a form. reset()is a Javascript function that works on form elements only. You can however define a new jquery function reset()that iterates through all form elements and calls the javascript reset()on each of them.

您需要单独重置每个元素。Jquery 没有reset()在表单上工作的函数。reset()是一个仅适用于表单元素的 Javascript 函数。但是,您可以定义一个新的 jquery 函数reset(),它遍历所有表单元素并reset()在每个元素上调用 javascript 。

$(document).ready(function(){
    $('a').click(function(){
        $('#reset').reset();
    });
});

 // we define a function reset
jQuery.fn.reset = function () {
  $(this).each (function() { this.reset(); });
}

Demo

演示

Alternatively, if you don't want to define a function, you can iterate through the form elements

或者,如果您不想定义函数,则可以遍历表单元素

$(document).ready(function() {
    $('a').click(function() {
        $('#reset').each(function() {
            this.reset();
        });
    });
});

Demo

演示

Source

来源

回答by Krishna Rani Sahoo

I followed the solution given by @sameera. But it still throw me error.

我遵循了@sameera 给出的解决方案。但它仍然让我出错。

I changed the reset to the following

我将重置更改为以下内容

$("form#frm_silder")[0].reset();

Then it worked fine.

然后它工作得很好。

回答by RainyTears

You can use the following.

您可以使用以下内容。

   @using (Html.BeginForm("MyAction", "MyController", new { area = "MyArea" }, FormMethod.Post, new { @class = "" }))
    {
        <div class="col-md-6">
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                @Html.LabelFor(m => m.MyData, new { @class = "col-form-label" })
            </div>
            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
                @Html.TextBoxFor(m => m.MyData, new { @class = "form-control" })
            </div>
        </div>
        <div class="col-md-6">
            <div class="">
                <button class="btn btn-primary" type="submit">Send</button>
                <button class="btn btn-danger" type="reset"> Clear</button>
            </div>
        </div>
    }

Then clear the form:

然后清除表格:

    $('.btn:reset').click(function (ev) {
        ev.preventDefault();
        $(this).closest('form').find("input").each(function(i, v) {
            $(this).val("");
        });
    });