2010-12-12 65 views
0

我在一個js文件有此功能禁用其他功能,一切都工作得很好:添加功能在js文件

function check_acco_form() 
{ 
    var name=$("#name").val(); 
     var institution=$("#institution").val(); 
     var year=$("#year").val(); 

     //PNR Data 
    var pnr1=$("#pnr1").val(); 
    var pnr2=$("#pnr2").val(); 
    // Arrival date info 
    var arr_year=$("#arr_year").val(); 
    var arr_month=$("#arr_month").val(); 
    var arr_date=$("#arr_date").val(); 
    //Departure date info 
    var dep_year=$("#dep_year").val(); 
    var dep_month=$("#dep_month").val(); 
    var dep_date=$("#dep_date").val(); 

    var numericExpression = /^[0-9]+$/; 

     //Name, institution and year must not be empty 
     if(name=="" || institution=="" || year=="") 
    { 
     alert("One or more fields are empty."); 
       return; 
     } 
    //PNR must be all numbers 
    if(!pnr1.match(numericExpression) || !pnr2.match(numericExpression)) 
    { 
     alert("A PNR number consists of 10 digits only. Please enter again."); 
     $("#pnr1").val(""); 
     $("#pnr2").val(""); 
     return; 
    } 
    if(pnr1.length!=3 || pnr2.length!=7) 
    { 
     alert('Invalid PNR Number.'); 
     $("#pnr1").val(""); 
     $("#pnr2").val(""); 
     return; 
    } 

    if((arr_month==dep_month && dep_date<arr_date) || (dep_month<arr_month)) 
    { 
     alert('Invalid dates.Please check again.'); 
     return; 
    } 

    //Test passed. Store in database; 
    URL="saveAcco.php"; 
    parameters="name="+name+"&ins="+institution+"&year="+year+"&pnr="+pnr1+""+pnr2+"&dateArr="+arr_year+"-"+arr_month+"-"+arr_date+"&dateDep="+dep_year+"-"+dep_month+"-"+dep_date; 
    $.get(URL+"?"+parameters,function(data){ 
     $("#msg_box").html(data); 
     if(data=="Your changes have been saved." || data=="Your data has been saved and is pending approval.") 
       { 
      $("#acco_status").html('<br/><b>Accomodation Approval Status</b> : <span style="padding:3px;background-color:#f4fb3c">Approval Pending</span><br/><br/>'); 
       } 
     $("#msg_box").fadeIn("slow",function(){ 
      setTimeout('fadeOutMsgBox();',3000); 
       }); 
      }); 
} 

我做了這個功能有點變化(增加了變量「mobile_num」和「train_num」,包括「如果」的條件,以確保用戶只輸入數字和修改了jQuery的獲取函數)這就造成了下面的代碼:

function check_acco_form() 
{ 
    //Personal Information 
    var name=$("#name").val(); 
    var institution=$("#institution").val(); 
    var year=$("#year").val(); 

    //Contact Information 
    var mobile_num=$("#mobile").val(); 

     //PNR Data 
    var pnr1=$("#pnr1").val(); 
    var pnr2=$("#pnr2").val(); 

    //Train Number 
    var train_num=$("#trainnum").val(); 

    // Arrival date info 
    var arr_year=$("#arr_year").val(); 
    var arr_month=$("#arr_month").val(); 
    var arr_date=$("#arr_date").val(); 
    //Departure date info 
    var dep_year=$("#dep_year").val(); 
    var dep_month=$("#dep_month").val(); 
    var dep_date=$("#dep_date").val(); 

    var numericExpression = /^[0-9]+$/; 

    //Name, institution and year must not be empty. 
    if(name=="" || institution=="" || year=="") 
    { 
     alert("One or more fields are empty."); 
     return; 
    } 

    //PNR can be empty but if entered must be all numbers 
    if(pnr1!="" and pnr2!="") 
    { 
     if(!pnr1.match(numericExpression) || !pnr2.match(numericExpression)) 
     { 
      alert("A PNR number consists of 10 digits only. Please enter again."); 
      $("#pnr1").val(""); 
      $("#pnr2").val(""); 
      return; 
     } 

     if(pnr1.length!=3 || pnr2.length!=7) 
     { 
      alert('Invalid PNR Number.'); 
      $("#pnr1").val(""); 
      $("#pnr2").val(""); 
      return; 
     } 
    } 

    //Train number can be empty but if entered must be all numbers 
    if(train_num!="") 
    { 
     if(!train_num.match(numericExpression)) 
     { 
      alert("Train number must consits of digits only"); 
      $("#trainnum").val(""); 
      return; 
     } 
    } 

    //Mobile num can be empty but must be all numbers 
    if(mobile_num!="") 
    { 
     if(!mobile_num.match(numericExpression)) 
     { 
      alert("Invalid mobile number"); 
      $("#mobile_num").val(""); 
      return; 
     } 
     if(mobile_num.length!=10) 
     { 
      alert('A mobile number consists of 10 digits.Please enter again.'); 
      return; 
     } 
    } 

    if((arr_month==dep_month && dep_date<arr_date) || (dep_month<arr_month)) 
    { 
     alert('Departure date cannot be before arrival date.Please check again.'); 
     return; 
    } 

    //Test passed. Store in database; 
    URL="saveAcco.php"; 
    parameters="name="+name+"&ins="+institution+"&year="+year+"&pnr="+pnr1+""+pnr2+"&dateArr="+arr_year+"-"+arr_month+"-"+arr_date+"&dateDep="+dep_year+"-"+dep_month+"-"+dep_date+"&mobile="+mobile_num+"&train_num="+train_num; 
    $.get(URL+"?"+parameters,function(data){ 
     $("#msg_box").html(data); 
     if(data=="Your changes have been saved." || data=="Your data has been saved and is pending approval.") 
       { 
      $("#acco_status").html('<br/><b>Accomodation Approval Status</b> : <span style="padding:3px;background-color:#f4fb3c">Approval Pending</span><br/><br/>'); 
      $("#acco_letter_print").html('Download accomodation letter <a href="PDF/acco_print.php" target="_blank">here</a>'); 
      $("#acco_letter_print").fadeIn();   
       } 
     $("#msg_box").fadeIn("slow",function(){ 
      setTimeout('fadeOutMsgBox();',3000); 
       }); 
      }); //End of get function 

} 

更改後,突然所有功能該功能的js文件已停止工作這個功能。在搜索論壇時,我發現這個討論:JavaScript function causing all other functions not to work inside js file其中說錯誤可能是由於使用保留字。但是,我無法在代碼中找到任何用作變量的保留字。任何想法可能是什麼問題?

+0

發佈針對兩者的'diff -u'運行的輸出。 – 2010-12-12 10:18:13

+1

如果你在瀏覽器中加載和運行腳本,它應該會給你一個錯誤信息... – 2010-12-12 10:22:29

回答

2

你在裏面有這樣的:

if(pnr1!="" and pnr2!="") 

它應該是:

if(pnr1!="" && pnr2!="") 

任何像這樣的語法錯誤會導致整個事情失敗,一定要檢查你的錯誤控制檯的東西像這樣,他們會很快指出原因。


作爲題外話,儘量不傳遞一個字符串到setTimeout()爲好,直接傳遞功能參考,改變此:

setTimeout('fadeOutMsgBox();',3000); 

向該:

setTimeout(fadeOutMsgBox,3000); 

這將給出更少的問題,並允許該函數處於範圍內的任何地方,它不必是全局的(就像字符串一樣)。