简体   繁体   English

提交后清除网络表单上的值

[英]Clear the values on a webform after submitting

I have a webform with few textboxes and dropdownlists, and a submit button 我有一个带有少量文本框和下拉列表的Web表单,还有一个提交按钮

when the user clicks the submit button im sending the form values to the controller with an ajax call and from controller to model to database 当用户单击“提交”按钮时,我使用ajax调用将表单值发送到控制器,并从控制器到模型再到数据库

after submitting the form values are still being displayed in the respective textboxes. 提交表单后,值仍会显示在相应的文本框中。

i also have a output parameter with my procedure, if the data is successfully submitted then i will get output parameter value as 1 else 0 我的过程中也有一个输出参数,如果数据提交成功,则输出参数值将为1,否则为0

so based on this output parameter value from the controller i should be able to clear the form values, if the insertion fails the values should be displayed 因此,基于控制器的此输出参数值,我应该能够清除表单值,如果插入失败,则应显示值

how to achieve it? 如何实现呢?

<script type="text/javascript">
    function CreateUser() {
        $.ajax({
            url: '@Url.Content("~/User/CreateUser")',
            data: { My Data },
            type: 'GET',
            success: function (data) {
                alert(data);
            }
        });
    }

    $('#btnCreate').click(function () {
        CreateUser();
    });
</script>

Code: 码:

public ActionResult RegisterUser(Users objUser)
{
    //i have done some code here to bind dropdownlists from database etc
    return View(objUser);
}

public ActionResult CreateUser(str Uname)
{
    try
    {
        Users objUser = new Users();
        int Successval = objUser.CreateUser(Uname);
    }
    catch (Exception ex)
    {
    }
    return RedirectToAction("RegisterUser");
}

try this ... 尝试这个 ...

after you get value 0 then just call below line 在您获得值0之后,只需在以下行调用

document.getElementById("YOUR_FORM_ID").reset();

Update 更新资料

View : 查看:

<form id="myform">
...
</form>

Javascript : Javascript:

function CreateUser() {

    $.ajax({
        url: '@Url.Content("~/User/CreateUser")',
        data: { My Data },

        type: 'GET',
        success: function (data) {
            if(data == 0)
              {
               document.getElementById("myform").reset();
              }

        }
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM