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

Using Ajax, textbox does not clear on submit in MVC

本文关键字:清除 文本 提交 Ajax MVC 使用      更新时间:2023-09-26

我需要你的帮助。我使用AjaxBeginform在一个小文本字段中提交一个问题。我可以提交问题,它会很好地发布到数据库,但问题永远不会解决。我使用Ajax是因为我不希望整个页面都发布。我试过使用这个.rest((;但这在IE中有效,但在Firefox和Chrome中不起作用。我试过$('#question'(.reset((;但这仍然不起作用。我确信这是我做得不对。

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

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

    <script type="text/javascript" src="~/Scripts/jquery-migrate-    1.2.1.min.js"></script>

这是AjaxBeginForm和文本框

     @using (Ajax.BeginForm("SendQuestion", new { PresId = Model.PresentationID, catalogId = ViewBag.CatalogId }, new AjaxOptions
        {
            HttpMethod = "Post",
            UpdateTargetId = "questionTxt",
            OnBegin = "OnBegin",
            OnSuccess = "OnSuccess",
            OnFailure = "OnFailure"
        }))
            {
                <fieldset id="question">
                    <p>
                        <textarea id="questionTxt" style="font-size:12px" cols="20" rows="6" name="questionTxt" onclick="javascript:removeTextAreaWhiteSpace(this);"></textarea>
                    </p>

                    <button id="question-btn" type="submit">Submit</button>
                </fieldset>
            }

这是我的OnSuccess功能

  function OnSuccess() {
    alert("Your question has been submitted.");
    $('#question').val('');
}

这是我的控制器

    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中清除。因此,当我提交表格时,弹出窗口会显示您的问题已提交,当弹出窗口可见时,问题变为真,然后单击弹出窗口上的"确定",IE中的文本框将被清除:

      function OnSuccess() {
    alert("Your question has been submitted.");
    $('#question').val('');
    this.reset();
}

如果有人能告诉我我做错了什么,我将不胜感激。非常感谢。

给表单一个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();
function OnSuccess() {
    alert("Your question has been submitted.");
    this.reset();
    $('#questionTxt').val(''); //Modified, put it AFTER reset()
}

问题Txt不是