2016-08-13 96 views
0

我正在使用以下脚本来自定义某些表单上的某些jQuery验证。这个脚本被调用一次,被一个模板文件用来生成几种不同形式之一,这意味着所有形式都被调用。当元素不存在时jquery失败

如果单独使用顶部(//确保有标题),所有表单都可以正常工作,因为所有表单都具有该标题元素,但其他两个验证(过期和开始/结束日期)仅适用于到其中一种形式,否则这些字段不存在。

我收到错误“(index):1966 Uncaught TypeError:尝试提交没有这些元素的表单时,无法读取属性'未定义'的属性,即使我添加了条件来检查这些元素元素第一。另外,验证jquery在这种情况发生时无法显示。

我在做什么错?

jQuery(document).ready(function($){ 
//Make the title required 
    acf.add_filter('validation_complete', function(json, $form){ 
     //Make sure there is a title 
     if(!$("#_post_title").val()) { 

      var temp = new Object(); 
      temp["input"] = "_post_title"; 
      temp["message"] = "A Title is required"; 
      json.errors.push(temp); 
     } 


     //Make sure the expiration is 3 months or less away (auditions) 
     if ($("#acf-field_574257a8eb3f0")!== null) { 
      var expRaw = $("input#acf-field_574257a8eb3f0").val(); 
      var expDate = expRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3') 
      var expiration = Date.parse(expDate); 
      var max = (3).months().fromNow(); 
      if (expiration > max) { 
       var temp = new Object(); 
       temp["input"] = "acf[field_574257a8eb3f0]"; 
       temp["message"] = "Maximum of 3 months from today!"; 
       json.errors.push(temp); 
      } 
     } 

     //Make sure start date is before end date and that start date is in the future 
     if ($("#acf-field_5701b4d1d11d0") !== null) { 
      var startRaw = $("input#acf-field_5701b4d1d11d0").val(); 
      var endRaw = $("input#acf-field_5701b4ecd11d1").val(); 
      var startDate = startRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3') 
      var endDate = endRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3') 
      var start = Date.parse(startDate); 
      var end = Date.parse(endDate); 
      if (start < Date.parse("now")) { 
       var temp = new Object(); 
       temp["input"] = "acf[field_5701b4d1d11d0]";//start date 
       temp["message"] = "Start date must be in the future."; 
       json.errors.push(temp); 
      } else if (start > end){ 
       var temp = new Object(); 
       temp["input"] = "acf[field_5701b4ecd11d1]";//end date 
       temp["message"] = "End date must be after start date."; 
       json.errors.push(temp); 
      } 
     } 


     // return 
     return json;    
    }); 
}); 

回答

1

尝试测试length - 如果返回0,那么它不通过if声明:

jQuery(document).ready(function($) { 
    //Make the title required 
    acf.add_filter('validation_complete', function(json, $form) { 
    //Make sure there is a title 
    if (!$("#_post_title").length) { 

     var temp = new Object(); 
     temp["input"] = "_post_title"; 
     temp["message"] = "A Title is required"; 
     json.errors.push(temp); 
    } 


    //Make sure the expiration is 3 months or less away (auditions) 
    if ($("#acf-field_574257a8eb3f0").length) { 
     var expRaw = $("input#acf-field_574257a8eb3f0").val(); 
     var expDate = expRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3') 
     var expiration = Date.parse(expDate); 
     var max = (3).months().fromNow(); 
     if (expiration > max) { 
     var temp = new Object(); 
     temp["input"] = "acf[field_574257a8eb3f0]"; 
     temp["message"] = "Maximum of 3 months from today!"; 
     json.errors.push(temp); 
     } 
    } 

    //Make sure start date is before end date and that start date is in the future 
    if ($("#acf-field_5701b4d1d11d0").length) { 
     var startRaw = $("input#acf-field_5701b4d1d11d0").val(); 
     var endRaw = $("input#acf-field_5701b4ecd11d1").val(); 
     var startDate = startRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3') 
     var endDate = endRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3') 
     var start = Date.parse(startDate); 
     var end = Date.parse(endDate); 
     if (start < Date.parse("now")) { 
     var temp = new Object(); 
     temp["input"] = "acf[field_5701b4d1d11d0]"; //start date 
     temp["message"] = "Start date must be in the future."; 
     json.errors.push(temp); 
     } else if (start > end) { 
     var temp = new Object(); 
     temp["input"] = "acf[field_5701b4ecd11d1]"; //end date 
     temp["message"] = "End date must be after start date."; 
     json.errors.push(temp); 
     } 
    } 


    // return 
    return json; 
    }); 
}); 
+0

谢谢,成功了! – Eckstein