Javascript 使用Ajax时,在MVC中提交时不会清除文本框

Javascript 使用Ajax时,在MVC中提交时不会清除文本框,javascript,jquery,html,ajax,ajax.beginform,Javascript,Jquery,Html,Ajax,Ajax.beginform,我需要你的帮助。我正在使用AjaxBeginform在一个小文本字段中提交一个问题。我可以提交问题,它可以向db发布罚款,但问题永远无法澄清。我使用Ajax是因为我不希望整个页面都发布。我试过使用这个。rest();但这在IE中有效,但在Firefox和Chrome中不起作用。我试过$('question').reset();但这仍然不起作用。我肯定这是我做得不对 下面是我的代码。谢谢你的帮助 这是我在页面顶部的脚本: <script type="text/javascript"

我需要你的帮助。我正在使用AjaxBeginform在一个小文本字段中提交一个问题。我可以提交问题,它可以向db发布罚款,但问题永远无法澄清。我使用Ajax是因为我不希望整个页面都发布。我试过使用这个。rest();但这在IE中有效,但在Firefox和Chrome中不起作用。我试过$('question').reset();但这仍然不起作用。我肯定这是我做得不对

下面是我的代码。谢谢你的帮助

这是我在页面顶部的脚本:

    <script type="text/javascript" src="~/Scripts/jquery-migrate-    1.2.1.min.js"></script>
这是我的控制器

    public bool SendQuestion(string questionTxt, long PresId, long catalogId)
    {
        try
        {
            var db = new ODTEntities();
            var pres = db.Presentations.FirstOrDefault(i => i.PresentationID == PresId);
            var subject = db.Catalogs.FirstOrDefault(i => i.CatalogID == catalogId).CatalogCode + " - " + pres.Title + " - " + pres.Presenter.PresenterName;
            Utils.AddQuestionToDB(db, PresId, catalogId, Utils.GetAttendeeId(), questionTxt);
            string body = "This question : " + Environment.NewLine + questionTxt + Environment.NewLine + " was sent by " +
                          User.Identity.Name;
            var catalog = db.Catalogs.FirstOrDefault(i => i.CatalogID == catalogId);
            if (!string.IsNullOrEmpty(catalog.QuestionsEmail))
                Helper.SendMail(body, subject, catalog.QuestionsEmail, "");
            else
                Helper.SendMail(body, subject);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
正如我之前所说的,我已经尝试过这个,它将在IE中清除,但不会在Firefox或Chrome中清除。因此,当我提交表单时,弹出窗口返回您的问题已提交,当弹出窗口可见时,问题变为true,然后在弹出窗口上单击ok,文本框在IE中被清除:

      function OnSuccess() {
    alert("Your question has been submitted.");
    $('#question').val('');
    this.reset();
}
如果有人能告诉我我做错了什么,我将不胜感激。多谢各位

function OnSuccess() {
    alert("Your question has been submitted.");

    this.reset();

    $('#questionTxt').val(''); //Modified, put it AFTER reset()
}

questionTxt问题为您的表单提供一个id,然后尝试重置它:

@using (Ajax.BeginForm("SendQuestion", new { PresId = Model.PresentationID, catalogId = ViewBag.CatalogId }, new AjaxOptions
    {
        HttpMethod = "Post",
        UpdateTargetId = "questionTxt",
        OnBegin = "OnBegin",
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure"

    },new {id="formid"}))
    }

$('#formid').reset();  // BY JQUERY
document.getElementById("formid").reset(); // BY PURE JAVASCRIPT
或者,如果页面上只有一个表单,那么这也将起作用,因为它仅适用于第一个表单:

$('form:first').reset();

我将它从问题改为问题文本,问题从文本框中消失,但出现了“真”一词。你的意思是“真”而不是问题?它当然来自
this.reset()可能尝试
函数OnSuccess(){alert(“您的问题已提交”);$('#questionTxt').val('');this.reset();$('#questionTxt').val('');//Modified}
我编辑我的答案。请重读它起作用了,我昨天把它作为解决方案勾掉了。非常感谢你。
@using (Ajax.BeginForm("SendQuestion", new { PresId = Model.PresentationID, catalogId = ViewBag.CatalogId }, new AjaxOptions
    {
        HttpMethod = "Post",
        UpdateTargetId = "questionTxt",
        OnBegin = "OnBegin",
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure"

    },new {id="formid"}))
    }

$('#formid').reset();  // BY JQUERY
document.getElementById("formid").reset(); // BY PURE JAVASCRIPT
$('form:first').reset();