提交后重置jquery移动表单输入

时间:2015-09-30 16:31:38

标签: jquery html forms jquery-mobile

我在jQuery移动框架中有一个表单,我希望在提交后重置输入字段的值。我试图在这里搜索解决方案,但我无法弄清楚如何让它正常工作。

jquery的:

$(document).ready(function () {
    $("#add").submit(function () {
        var formData = $(this).serialize();

        $.post('save.php', formData, processData).error(errorResponse);

        function processData(data) {
            $("#popupSave").popup();
            $("#popupSave").popup("open");
        };
        function errorResponse() {
            alert("Something went wrong!");
        };
        return false;
    });
});

形式:

<form id="add">
  <div data-controltype="textblock">
    <p>
      Text
    </p>
  </div>
  <div class="ui-field-contain" data-controltype="textinput">
    <input name="vegetables" id="textinput1" placeholder="300g" value="" type="number">
  </div>
  <div data-controltype="textblock">
    <p>
      Text
    </p>
  </div>
  <div class="ui-field-contain" data-controltype="textinput">
    <input name="fullgrain" id="textinput2" placeholder="30g" value="" type="number">
  </div>
  <div data-controltype="textblock">
    <p>
      Text
    </p>
  </div>
  <div class="ui-field-contain" data-controltype="textinput">
    <input name="milk" id="textinput3" placeholder="200ml" value="" type="number">
  </div>
  <div data-controltype="textblock">
    <p>
      Text
    </p>
  </div>
  <div class="ui-field-contain" data-controltype="textinput">
    <input name="water" id="textinput4" placeholder="300ml" value="" type="number">
  </div>
  <input type="submit" value="Tilføj">
  <input type="reset" value="Nulstil" />
  <div data-role="popup" id="popupSave" class="ui-content">
    <p>Text</p>
  </div>
</form>

2 个答案:

答案 0 :(得分:3)

然后试试这个:

$(document).ready(function () {
    $("#add").submit(function () {
        $this = $(this);
        var formData = $this.serialize();

        $.post('save.php', formData, function (data) {
            $("#popupSave").popup();
            $("#popupSave").popup("open");
            $this.find("input").val("");
        }).error(errorResponse);

        function errorResponse() {
            alert("Something went wrong!");
        };
        return false;
    });
});

答案 1 :(得分:0)

您可以将其用于所有类型的输入

$('input').not('[type="button"]').val(''); // clear inputs except buttons, setting value to blank
$('select').val(''); // clear select
$('textarea').val(''); // set text area value to blank