2016-08-13 90 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