2013-07-18 46 views
0

我有以下兩個的DateTimePicker功能/字段:爲什麼是JavaScript的傳遞非驗證信息

$(function() 
    { 
     $('#updateJobForm').validate(); 
     var startDateTime = $('#startTime'); 
     var endDateTime = $('#endTime'); 
     var currentDate = new Date(); 
     var orginalDate = startDateTime.val(); 

     startDateTime.datetimepicker(
      { 
       timeFormat: "hh:mm tt", 
       minDate: ("setDate", '',new Date()), 
       hourGrid: 12, 
       minuteGrid: 15, 
       stepMinute: 15, 
       onClose: function(dateText, inst) 
       { 
        if (endDateTime.val() != '') 
        { 
         var testStartTime = startDateTime.datetimepicker('getDate'); 
         var testEndTime = endDateTime.datetimepicker('getDate'); 
         if (testStartTime > testEndTime) 
         { 
          alert("The start time cannot be greater than the end time."); 
          //startDateTime.val(currentDate.format("m/d/yyyy hh:mm tt")); 
         } 
        } 
        else 
        { 
         endDateTime.val(dateText); 
        } 
       }, 
       onSelect: function (selectedDateTime) 
        { 
         var testStartTime = startDateTime.datetimepicker('getDate'); 
         var testEndTime = endDateTime.datetimepicker('getDate'); 
         //if(endDateTime.val() == '') 
         //{ 
          endDateTime.datetimepicker('option', 'minDate', startDateTime.datetimepicker('getDate')); 
        } 
       } 
      ); 
      endDateTime.datetimepicker(
       { 
        minDate: ("setDate", '',new Date()), 
        timeFormat: "hh:mm tt", 
        hourGrid: 12, 
        minuteGrid: 15, 
        stepMinute: 15, 
        onClose: function(dateText, inst) 
        { 
         if (startDateTime.val() != '') 
         { 
          //var testStartTime = startDateTime.datetimepicker('getDate'); 
          var testEndTime = endDateTime.datetimepicker('getDate'); 
          //if (testEndTime < testStartTime) 
          //{ 
          //alert("The end date blah blah"); // and time must be greater than the start date and time"); 
          //endDateTime.val(testStartTime.format("mm/dd/yyyy hh:MM")); 
          //} 
          //else 
          endDateTime.datetimepicker('setDate', testEndTime); 
         } 
         //else 
         { 
         // startDateTime.val(dateText); 
         } 
        }, 
        onSelect: function (selectedDateTime) 
        { 
        startDateTime.datetimepicker('option', 'maxDate', endDateTime.datetimepicker('getDate')); 
        } 
       } 
      ); 
     } 
    ); 
    function validate_form() 
    { 
     var sDate= new Date(document.updateJobForm.startTime.value); 
     var eDate= new Date(document.updateJobForm.endTime.value); 
     if ((document. updateJobForm.startTime.value != "") && (document. updateJobForm.endTime.value != "") && (sDate < eDate)) 

     { 
      return true; 
     } 

     else 

     { 
      alert("Please choose a start date/time that begins before the end date/time"); 
      return false; 
     } 
    } 

而且一個是從我所謂的提交按鈕,看起來像這樣的validate_form功能:

<td align="right"><input name="reset" type="reset" value="Reset" /> 
<input name="submit" type="submit" value="Update" onclick="validate_form()"/></td> 
<td>&nbsp;</td> 

當我點擊提交按鈕(錯誤的數據,即開始日期比結束日期更大)時,我會收到提醒消息,告訴用戶更正時間。用戶單擊確定提醒後,表單會提交無效數據。該函數看起來是正確的,邏輯似乎工作,但爲什麼函數返回false後提交表單?更糟糕的是,如果數據是正確的,表單根本不會提交。出了什麼問題,我該如何解決這個問題?

回答

2

你錯過了你的按鈕返回

onclick="return validate_form()" 
+0

謝謝你是對的。我是JavaScript新手,非常感謝您的幫助! – royjm

0

你的驗證功能應該放在形式本身的「的onsubmit」事件,而不是提交按鈕的「點擊」事件!這就是返回值阻止表單提交的地方!