form表单提交之前推断

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事件无法触发,使用绑定button点击事件取代
    function check() {
    	alert("check");
		var excel_file = $("#excel_file").val();
		if (excel_file == "" || excel_file.length == 0) {
			alert("请选择文件路径。");
			return false;
		} else {
            return true;
        }
	}

2.使用button事件绑定


$("#excel_button").on('click', function() {
    	var excel_file = $("#excel_file").val();
		if (excel_file == "" || excel_file.length == 0) {
			alert("请选择文件路径!");
			return false;
		} else {
            return true;
        }
    });


原文地址:https://www.cnblogs.com/bhlsheji/p/5325666.html