1

处理表单提交后调用的函数。我正在使用 JQuery 1.8 和 jquery.validate。

我有以下函数来检查和处理对服务器的调用并接收 JSON 成功回调:

<input type="submit" value="Continue" 
                      name="submit_first" onclick="verifyPge();" />

这是我需要添加加载页面的函数,它需要在同一个窗口中加载(JQuery 加载页面)这个函数使用 jquery.validate 运行 UI 检查,我需要向这个 verifyPge 添加一个新函数( ); 函数,以便表单加载新页面。使用该方法或操作不是一个选项,并且正在对表单 ID 调用验证以使其保持独立。
// JavaScript 文档 $().ready(function() {

// validate signup form on keyup and submit
$("#yourInfo").validate({

    rules: {
        month: {
            required: true,
        },
        day: {
            required: true,
        },
        year: {
            required: true
        },

    },
    messages: {
        month: {
            required: "Please select the month you were born",
        },
        day: {
            required: "Please select the day you were born",
        },
        year: {
            required: "Please select the year you were born",
        },
        errorElement: "div"
    }
});

});

有任何想法吗?基本上,在完成 UI 验证并处理表单后,我想加载新页面,但使用 JQuery 页面加载并让 Jquery 通过 Ajax 切换页面。

4

1 回答 1

0

确保在您的服务器中返回 URL

// document DOM ready
$(function() {

// validate signup form on keyup and submit
$("#yourInfo").validate({
  submitHandler: function(form){
    // submit the form via ajax
    $.post(
      // your script
      'script.php',
      // your form in a serialized way your server can understand
      form.serializeArray()[0], 
      // success function
      function(url){
        window.location.assign(url); // load the new URL
      }
    );
    return false;
  },
  rules: {
    month: {
        required: true,
    },
    day: {
        required: true,
    },
    year: {
        required: true
    },
  },
  messages: {
    month: {
        required: "Please select the month you were born",
    },
    day: {
        required: "Please select the day you were born",
    },
    year: {
        required: "Please select the year you were born",
    },
    errorElement: "div"
  }
});

});

jQuery 验证插件已经处理了您的表单提交,无需verifyPage()在提交按钮中添加。使用submitHandler是表单验证后提交的正确方法。

于 2012-12-07T02:16:11.570 回答