2017-04-11 126 views
-4

我試圖用該Java腳本提交我的表單。但它沒有按原樣運行,它沒有從腳本中獲得價值。它只適用於我的HTML體內的警報編碼,但它沒有讀取函數check(),我的表單是否有一些問題?我的提交按鈕不起作用

<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> 

這是我的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

好吧,您不要取消點擊 – epascarello

+0

標記中刪除'onsubmit =「return check()」'。將check()事件處理程序添加到按鈕按下。使用AJAX手動提交或添加表單POST目標和'form.submit()'您可能無法從check()處理程序返回以停止發生提交。所以用像if(isValid)form.submit()'ps這樣的邏輯來重新使用它:你可以遍歷所有這些值來檢查它們是不是空字符串。 – Shilly

回答

1

檢查下面的工作代碼:

 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

謝謝,它突然在我的瀏覽器中工作。你只需在javascript中添加警報吧? – user3455084

+0

問題出現在這一行var isi4 = document.forms [「myForm」] [「tel」] .value;錯誤的名字「tel」 –