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.     });  


 

 
posted @ 2017-10-17 14:27  ma_fighting  阅读(10360)  评论(0编辑  收藏  举报
历史天气查