重置ajax提交上的文本字段

时间:2014-01-15 13:19:24

标签: javascript jquery ajax

我的代码没有将字段重置为空:

  <script type="text/javascript">
         $(function () {
             $('#ProblemButton').click(function (event) {
                 var form = $('#Form1');
                 $.ajax({

                     type: form.attr('method'),
                     url: form.attr('action'),
                     data: $("#reportProblem :input").serialize(),
                     cache: false,

                 }).done(function (data) {
                     $('#reportProblem').modal('hide');

                     if (data.ResponseType == 0) {
                         $('#SuccessMsg').text('Problem Reported');
                         $('#SuccessMessage').modal('show');

                     }
                     else if (data.ResponseType == 1) {
                         $("#SuccessMsg").attr("class", "alert alert-danger");
                         $('#SuccessMsg').text('Can not report problem');
                         $('#SuccessMessage').modal('show');
                     }

                     else {
                         $('#SuccessMsg').text('Data: ' + data.ResponseType + $("#reportProblem :input").serialize());
                         $('#SuccessMessage').modal('show');
                     }
                     $('#problemDescription').attr('value', '');

                     // Optionally alert the user of success here...


                     setTimeout(function () { $('#SuccessMessage').modal('hide'); }, 3000);


                 }).fail(function (jqXHR, textStatus) {
                     // Optionally alert the user of an error here...
                     alert("Error submitting AJAX request" + textStatus);
                 });
                 event.preventDefault(); // Prevent the form from submitting via the browser.
             });
         });

内容不足:

       $('#problemDescription').attr('value', '');

HTML元素在这里:

     <asp:TextBox TextMode="MultiLine" ID="problemDescription" Columns="80" Rows="5" runat="server" />

1 个答案:

答案 0 :(得分:2)

您的问题是您错过了ClientIDMode="Static",因为控件是ASP.net控件且ID正在更改:

<asp:TextBox TextMode="MultiLine" ID="problemDescription" Columns="80" Rows="5" runat="server" ClientIDMode="Static"/>

您可以使用:

$('#problemDescription').val('');