2017-04-11 4 views
-4

J'ai essayé de soumettre mon formulaire avec ce script java. Mais ça n'a pas marché comme il se doit, ça n'a pas la valeur du script. ça marche seulement pour le code d'alerte dans mon corps html, mais il n'a pas lu la fonction check(), est-ce que ma forme a un problème?Mon bouton de soumission ne fonctionne pas

<form name="myForm" method="post" onsubmit="return check()"> 
         <table width="100%"> 
         <tr> 
          <div id="space"> 
           YOUR DETAILS 
          </div><!-- space --> 
         </tr> 
         <tr> 
          <td width="186"><span style="color:red;">*</span>First Name</td> 
          <td width="720"> 
          <input type="text" name="fname"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186"><span style="color:red;">*</span>Last Name</td> 
          <td width="720"> 
          <input type="text" name="lname"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186"><span style="color:red;">*</span>Email</td> 
          <td width="720"> 
          <input type="email" name="email"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186"><span style="color:red;">*</span>Telephone</td> 
          <td width="720"> 
          <input type="tel" name="telephone"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186">Fax</td> 
          <td width="720"> 
          <input type="tel" name="fax"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186">Company</td> 
          <td width="720"> 
          <input type="text" name="company"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186">Company ID</td> 
          <td width="720"> 
          <input type="text" name="cid"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186"><span style="color:red;">*</span>Address 1</td> 
          <td width="720"> 
          <input type="text" name="add1"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186">Address 2</td> 
          <td width="720"> 
          <input type="text" name="add2"> 
          </td> 
         </tr> 
         <tr> 
          <td width="186"><span style="color:red;">*</span>City</td> 
          <td width="720"> 
          <input type="text" name="city" > 
          </td> 
         </tr> 
         <tr> 
          <td width="186">Poscode</td> 
          <td width="720"> 
          <input type="text" name="poscode" > 
          </td> 
         </tr> 
         <tr> 
          <td width="186"><span style="color:red;">*</span>Country</td> 
          <td width="720"> 
           <select name="country"> 
            <option value="malaysia">Malaysia</option> 
            <option value="australia">Australia</option> 
            <option value="japan">Japan</option> 
            <option value="newzealand">New Zealand</option> 
           </select> 
          </td> 
         </tr> 
         </table> 

         <br><br> 

         <input type="button" onclick="check()" name="submit" value="Submit" class="sbutton" /> 
         <button name="subcancel" class="sbutton" value="Cancel" >CANCEL</button> 
         </form> 

C'est ma fonction javascript

function check(){ 
alert('hi'); 
var isi1=document.forms["myForm"]["fname"].value; 
var isi2=document.forms["myForm"]["lname"].value; 
var isi3=document.forms["myForm"]["email"].value; 
var isi4=document.forms["myForm"]["tel"].value; 
var isi5=document.forms["myForm"]["add1"].value; 
var isi6=document.forms["myForm"]["city"].value; 
var isi7=document.forms["myForm"]["country"].value; 

if (isi1 == "") { 
    alert("Please complete all your detail with '*' symbol!"); 
    return false; 
} 
else if(isi2 ==""){ 
    alert("Please complete your detail!"); 
    return false; 
} 
else if(isi3 ==""){ 
    alert("Please complete your detail!"); 
    return false; 
} 
else if(isi4 ==""){ 
    alert("Please complete your detail!"); 
    return false; 
} 
else if(isi5 ==""){ 
    alert("Please complete your detail!"); 
    return false; 
} 
else if(isi6 ==""){ 
    alert("Please complete your detail!"); 
    return false; 
} 
else{ 
    alert("Hi "+isi1+" "+isi2+"!! You are succesfully registered to our bookstore!!"); 
    return true; 
} 

}

+0

Javascri pt! = Java –

+0

Bien, vous n'annulez pas le clic – epascarello

+0

Supprimez 'onsubmit =" return check() "' de la balise . Ajoutez le gestionnaire d'événement check() à la pression du bouton. Effectuez un envoi manuel avec AJAX ou ajoutez un formulaire POST target et 'form.submit()' Vous ne pouvez probablement pas revenir du gestionnaire check() pour arrêter l'envoi. Donc, répétez-le avec une logique comme 'if (isValid) form.submit()' ps: vous pouvez faire une boucle sur toutes ces valeurs à la place pour vérifier si elles ne sont pas des chaînes vides. – Shilly

Répondre

1

Vérifiez le code de travail ci-dessous:

 function check() { 
 
     alert('hi'); 
 
     var isi1 = document.forms["myForm"]["fname"].value; 
 
     var isi2 = document.forms["myForm"]["lname"].value; 
 
     var isi3 = document.forms["myForm"]["email"].value; 
 
     var isi4 = document.forms["myForm"]["telephone"].value; 
 
     var isi5 = document.forms["myForm"]["add1"].value; 
 
     var isi6 = document.forms["myForm"]["city"].value; 
 
     var isi7 = document.forms["myForm"]["country"].value; 
 

 
     if (isi1 == "") { 
 
      alert("Please complete all your detail with '*' symbol!"); 
 
      return false; 
 
     } 
 
     else if (isi2 == "") { 
 
      alert("Please complete your detail!"); 
 
      return false; 
 
     } 
 
     else if (isi3 == "") { 
 
      alert("Please complete your detail!"); 
 
      return false; 
 
     } 
 
     else if (isi4 == "") { 
 
      alert("Please complete your detail!"); 
 
      return false; 
 
     } 
 
     else if (isi5 == "") { 
 
      alert("Please complete your detail!"); 
 
      return false; 
 
     } 
 
     else if (isi6 == "") { 
 
      alert("Please complete your detail!"); 
 
      return false; 
 
     } 
 
     else { 
 
      alert("Hi " + isi1 + " " + isi2 + "!! You are succesfully registered to our bookstore!!"); 
 
      return true; 
 
     } 
 
    }
<form name="myForm" method="post" onsubmit="return check()"> 
 
<table width="100%"> 
 
    <tr> 
 
     <div id="space"> 
 
      YOUR DETAILS 
 
     </div><!-- space --> 
 
    </tr> 
 
    <tr> 
 
     <td width="186"><span style="color:red;">*</span>First Name</td> 
 
     <td width="720"> 
 
      <input type="text" name="fname"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186"><span style="color:red;">*</span>Last Name</td> 
 
     <td width="720"> 
 
      <input type="text" name="lname"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186"><span style="color:red;">*</span>Email</td> 
 
     <td width="720"> 
 
      <input type="email" name="email"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186"><span style="color:red;">*</span>Telephone</td> 
 
     <td width="720"> 
 
      <input type="tel" name="telephone"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186">Fax</td> 
 
     <td width="720"> 
 
      <input type="tel" name="fax"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186">Company</td> 
 
     <td width="720"> 
 
      <input type="text" name="company"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186">Company ID</td> 
 
     <td width="720"> 
 
      <input type="text" name="cid"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186"><span style="color:red;">*</span>Address 1</td> 
 
     <td width="720"> 
 
      <input type="text" name="add1"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186">Address 2</td> 
 
     <td width="720"> 
 
      <input type="text" name="add2"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186"><span style="color:red;">*</span>City</td> 
 
     <td width="720"> 
 
      <input type="text" name="city"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186">Poscode</td> 
 
     <td width="720"> 
 
      <input type="text" name="poscode"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width="186"><span style="color:red;">*</span>Country</td> 
 
     <td width="720"> 
 
      <select name="country"> 
 
       <option value="malaysia">Malaysia</option> 
 
       <option value="australia">Australia</option> 
 
       <option value="japan">Japan</option> 
 
       <option value="newzealand">New Zealand</option> 
 
      </select> 
 
     </td> 
 
    </tr> 
 
</table> 
 

 
<br><br> 
 

 
<input type="button" onclick="check()" name="submit" value="Submit" class="sbutton" /> 
 
<button name="subcancel" class="sbutton" value="Cancel">CANCEL</button> 
 
</form>

+0

merci, il fonctionne soudainement dans mon navigateur. Vous venez d'ajouter l'alerte à javascript non? – user3455084

+0

problème était sur cette ligne var isi4 = document.forms ["myForm"] ["tel"]. Value; mauvais nom "tel" –