2016-08-05 116 views
0

我有得到在弹出交替显示两种形式,一种为编辑(这是由AJAX加载),一个创造。如何处理一个页面上的两个表单进行验证?

我想使用jQuery验证同时显示在编辑和提交字段提示。

一些验证涉及的时间跨度不得重复。

这适用于创作,但我创建用于编辑的新规则并没有被触发,因为创建规则优先。

我加入类规则,因为规则我创建通过名称或ID不知何故没有得到积极的所有,也许是因为我感到困惑的JQuery验证插件如何工作。这些规则在代码片段中被注释掉了。

var classRules = {}; 

classRules['thresholdStartTime'] = {}; 
classRules['thresholdStartTime'][globalThresholdCheckName] = $form 
classRules['thresholdEndTime'] = {}; 
classRules['thresholdEndTime'][globalThresholdCheckName] = $form 

/* 
* 
for(var q = 0; q<20; ++q) { 
    validationrules['thresholdStartTime' + q] = {}; 
    validationrules['thresholdStartTime' + q]['required'] = true; 
    validationrules['thresholdStartTime' + q][globalThresholdCheckName] = $form 

    validationrules['thresholdEndTime' + q] = {}; 
    validationrules['thresholdEndTime' + q]['required'] = true; 
    validationrules['thresholdEndTime' + q][globalThresholdCheckName] = $form 

} 
*/ 

for(var cls in classRules) { 
    var rule = classRules[cls]; 
    $.validator.addClassRules(cls, rule); 
} 

//return validationrules as rm.rules here 

//in the caller, get validationrules from result 
var editForm = $('#editForm') 
.validate(
     { 
      rules : rm.rules, 
      classRules: rm.classRules, 
      messages : rm.messages, 
      success : $.noop 
     }); 
+0

感谢大家帮助。看起来验证首次配置时会首选,因此忽略同一表单的第二条验证命令。 – Adder

回答

1

使该表单的默认操作是防止e.preventDefault(),然后提交使用jQuery.ajax(),选择什么data发送,然后绑定该ajax请求表单的submit按钮。这样做对每种形式:

jQuery('.bidvalue').click(function(e) { 
    e.preventDefault(); 
    jQuery.ajax({ 
     type: 'POST', 
     url: ajaxurl, 
     data: 'action=newbid&id='+e.target.name, 
     success: function(msg){ 
      jQuery('#vehicle-value-box'+e.target.name).html(msg+',00€'); 
     } 
}); 

编辑,因为我完全错过了实际问题...

为每个表单,您可以指定验证如下:

$('#myform1').validate({ 
    rules: { 
     myfield1: { 
      required: true, 
      minlength: 3 
     }, 
     myfield2: { 
      required: true, 
      email: true 
     } 
    } 
}); 

但您必须更具体,并显示两种形式的HTML,也许你想要什么验证规则,f或者我真的很有帮助地回答你的问题。

编辑#2:从评论中添加一些东西(荣誉给Vicente Olivert Riera输入)。

如果你想切换何种形式在申请验证,这样做:

var activeForm; 

$("form").focus(e => { 
    e.validate({ 
     rules: { 
      myfield1: { 
       required: true, 
       minlength: 3 
      }, 
      myfield2: { 
       required: true, 
       email: true 
      } 
     } 
    }); 
}); 

如果你有很多形式,只是想只有符合特定条件的具体形式:

$("form").focus(e => { 
    // Some criteria to check against for the form. 
    // Obviously you could make a crafty selector and avoid `if (e.hasClass())`, 
    // but this is to demonstrate using if to test the form if you wanted to check the form element in any way before applying `.validate()`. 
    if (e.hasClass("someClass") { 
     e.validate(...); 
    } 
}); 
+0

这和我的问题有关吗? – Adder

+0

...你想在同一页面上提交多个表单吗? –

+0

不,我想有同时启动一个形式中,并且具有在表单字段验证是棘手的部分。 – Adder

相关问题