【问题标题】:Jquery Form Plugin - handling error callbackJquery Form Plugin - 处理错误回调
【发布时间】:2011-05-31 19:44:59
【问题描述】:

我正在使用一个 jQuery 表单插件 link,效果很好。一切都很简单,工作正常。但是现在我想添加一个验证并处理一个错误回调..而且没有选择!!可能的选项是:

target:        '#output1',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
    success:       showResponse  // post-submit callback 

    // other available options: 
    //url:       url         // override for form's 'action' attribute 
    //type:      type        // 'get' or 'post', override for form's 'method' attribute 
    //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
    //clearForm: true        // clear all form fields after successful submit 
    //resetForm: true        // reset the form after successful submit 

    // $.ajax options can be used here too, for example: 
    //timeout:   3000 

所以我不知道我的表格是否有效。现在我在想我应该使用 JSON 并在我的成功函数中添加这样的内容:

例如电子邮件验证:

function finish(data) (
  if (data.email != null) $("#emailErrorDiv").html(data.email);
  .......
  if (data.success != null) $("#successDiv").html(data.success);
)

这是一个好的解决方案吗?我希望有可能以更简单的方式实现这一点。

【问题讨论】:

    标签: jquery ajax validation callback


    【解决方案1】:

    在beforeSubmit 回调中验证。返回false 以阻止发送表单。

    编辑: jQuery Form 有一个验证示例 - jQuery Form Validation

    【讨论】:

    • 我想在服务器端验证我的表单
    • 但是您的示例验证是在 jQuery 中(即客户端)
    • DATA 对象是从我的 .java 控制器返回的 JSON 对象。因此 data.email != null 检查电子邮件字段是否存在验证错误,以及是否有错误消息(例如:电子邮件不得为空)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2016-12-21
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多