提交后无法正常刷新jquery表单

时间:2016-06-27 06:03:15

标签: javascript php jquery html forms

对于我的一个网站,我正在设计一个带有jquery和PHP的HTML表单,用于通过电子邮件发送数据。 Jquery,检查空/空白字段并发送每个字段的警报。 一旦表单提交完整数据,它就会发送成功警报并通过PHP发送输入数据 直到这一点,代码工作正常。

提交数据后&显示成功警报,我希望刷新/重新加载html表单。 但这种情况并没有发生,因为表格仍然是预先输入的数据 请帮助我。我的Jquery& HTML表单如下所示。

JQUERY-CODE(使用google libs 2.2)

JQUERY CODE

 $(document).ready(function() {
      $("#submit").click(function() {
        var name = $("#name").val();
        var email = $("#email").val();
        var message = $("#message").val();
        $("#returnmessage").empty(); // To empty previous error/success message.
        // Checking for blank fields.
        if (name == '') {
          alert(" Please fill your name");
        } else if (email == '') {
          alert("Please fill your email");
        } else if (message == '') {
          alert("Please Fill message");
        } else { alert("Your form submitted. We will contact you soon.");
          $.post("xxx.php", { // To php file.
            name: name,
            email: email,
            message: message
          }, function(data) {
            $("#returnmessage").append(data);
            if (data == "Your form submitted. We will contact you soon.") {
              $("#form")[0].reset(); // To reset form fields on success.
            }
          });
        }
      });
    });

HTML FORM CODE:

<form id="form" method="post" action="  ">
  <h4>FEED-BACK FORM</h4>
  <p id="returnmessage"></p>
  <label>Name:  </label>
  <input type="text" id="name" /><br>
  <label>Email:  </label>
  <input type="text" id="email" /><br>
  <label>Message:  </label>
  <input type="text" id="message" /><br>
  <input type="button" id="submit" value="Submit"/>
</form>

3 个答案:

答案 0 :(得分:2)

检查以下js

# ... is just a place holder token that can't be None
# as I can't write what I want, (self, key_node=self._front)
# as self isn't defined at this point!

def clean_recursive(self, key_node=...):

    if key_node is ...:
        key_node = self._front
    elif key_node is None:
        return

    previous = key_node
    current = key_node._next

    while current is not None:
        if current._value == key_node._value:
            # Same situation as when we started, clean
            # rest of list; toss current node & quit loop
            self.clean_recursive(current)
            previous._next = current._next
            self._count -= 1
            break

        previous = current
        current = current._next

    self.clean_recursive(key_node._next)  # check the 2nd, 3rd, etc. element

}

或者

 if (data == "Your form submitted. We will contact you soon.") {
        document.getElementById('form').reset(); // To reset form fields on success.

答案 1 :(得分:0)

您可以通过将输入字段的值设置为""

来清除输入字段
 $("#name").val("");
$("#email").val("");
$("#message").val("");

答案 2 :(得分:0)

问题很可能出在if声明中。行$("#form")[0].reset();是正确的,将其移到if语句上方以使其正常工作。