1.使用onsubmit方法
<form name="Form" action="t" method="post" onsubmit="return check();"> <input class="btn btn-default" id="excel_file" type="file" name="filename" accept="xls"/> <input id="excel_button" type="submit" value="导入Excel"/> </form>
//onsubmit事件无法触发,使用绑定按钮点击事件代替 function check() { alert("check"); var excel_file = $("#excel_file").val(); if (excel_file == "" || excel_file.length == 0) { alert("请选择文件路径!"); return false; } else { return true; } }
$("#excel_button").on('click', function() { var excel_file = $("#excel_file").val(); if (excel_file == "" || excel_file.length == 0) { alert("请选择文件路径!"); return false; } else { return true; } });
原文地址:http://blog.csdn.net/kingson_wu/article/details/38928827