2010-12-12 4 views
0

J'ai eu cette fonction dans un fichier js et tout fonctionnait très bien:fonction Ajout est invalidante d'autres fonctions dans le fichier 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); 
       }); 
      }); 
} 

J'ai fait un peu de changements à cette fonction (ajoutée de mobile_num 'variables et « train_num », inclus « si » les conditions pour vous assurer que l'utilisateur entre des chiffres et a apporté des modifications à la fonction obtenir jQuery) ce qui a donné lieu dans le code suivant:

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 

} 

Après les changements, tout à coup toutes les fonctions le fichier js de cette fonction a cessé de fonctionner y compris cette fonction. Lors de la recherche sur le forum, j'ai trouvé cette discussion: JavaScript function causing all other functions not to work inside js file qui dit que l'erreur peut être due à l'utilisation de mots réservés. Cependant, je ne trouve aucun mot réservé utilisé comme variable dans mon code. Des idées quel peut être le problème?

+0

Afficher la sortie de l'exécution 'diff -u' dans les deux cas. –

+1

Si vous chargez et exécutez le script dans un navigateur, il devrait vous donner un message d'erreur ... –

Répondre

2

Vous avez ce là:

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

Il devrait être:

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

Toute erreur de syntaxe comme celle-ci provoqueront la chose entière à l'échec, assurez-vous de vérifier votre console d'erreur pour les choses comme ça, ils vont rapidement indiquer la cause.


En aparté, essayez de ne pas passer une chaîne à setTimeout() ainsi, passer la référence de fonction directement, de changer cette situation

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

à ceci:

setTimeout(fadeOutMsgBox,3000); 

Cette volonté Donner moins de problèmes, et permettre à la fonction d'être n'importe où dans la portée, il ne doit pas être global (comme ce serait le cas avec une chaîne).

Questions connexes