form表單提交之前判斷


1.使用onsubmit方法

 

  1. <form name="Form" action="t" method="post" onsubmit="return check();">  
  2. <input class="btn btn-default" id="excel_file" type="file" name="filename"  accept="xls"/>  
  3. <input  id="excel_button" type="submit" value="導入Excel"/>  
  4. </form>  

  1. //onsubmit事件無法觸發,使用綁定按鈕點擊事件代替  
  2.    function check() {  
  3.     alert("check");  
  4.     var excel_file = $("#excel_file").val();  
  5.     if (excel_file == "" || excel_file.length == 0) {  
  6.         alert("請選擇文件路徑!");  
  7.         return false;  
  8.     } else {  
  9.            return true;  
  10.        }  
  11. }  

2.使用按鈕事件綁定

 

  1. $("#excel_button").on('click', function() {  
  2.         var excel_file = $("#excel_file").val();  
  3.         if (excel_file == "" || excel_file.length == 0) {  
  4.             alert("請選擇文件路徑!");  
  5.             return false;  
  6.         } else {  
  7.             return true;  
  8.         }  
  9.     });  


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM