jQuery Ajax提交问题

时间:2011-06-18 12:28:37

标签: forms jquery-plugins jquery

表格

  <article id="signup-form">

    <form  id="signupform" method="post" action="signup.php" accept-charset="utf-8">
      <label for="email">Email Address</label>
      <input type="text" id="email" name="email"/>
      <input type="image" src="images/btn_signup.png" value="Sign up" alt="Sign up"/>
    </form>
    <br />
  </article>

Javascript

$(document).ready(function () {

    $('#signupform').submit(function () {
        var email = $('#email').val();

        if (email != 0) {
            if (isValidEmailAddress(email)) {
                $(this).ajaxSubmit(options);
            } else {
                alert("Please Enter a Valid Email Address");
            }
        } else {
            alert("Please Enter a an Email Address");
        }

        return false;
    });

    var options = {
        success: showResponse,
        dataType: 'json',
        resetForm: true
    };

    function showResponse(responseText, statusText, xhr, $form) {
        if (responseText == 'false') {
            alert("Please try again. We could not sign you up.");
        }
        if (responseText == 'true') {
            alert("Success");
        }
    }
});


function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

我收到以下错误,表单提交并转到signup.php

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过更改通话来阻止表单默认事件 $('#signupform').submit(function (event)和进入event.preventDefault