<iframe id="uploadframe" width="0" height="0" style="display:none;" name="upload_target"></iframe> <form id="uploadpic" target="upload_target" enctype="multipart/form-data" method="post" action="" style="display: none;"> <input type="file" name="uploadImg" id="uploadImg"> </form> $(function(){ $("#uploadImg").change(function(){ if($(this).val() && checkFile('uploadImg')){ $('#uploadpic').submit(); $(this).val(''); return true; //$('#uploadpic').submit(); } return false; }); }); function checkFile(id) { extensions = 'jpg,gif,png,jpeg'; var path = document.getElementByIdx_x_x_x_x_x_x(id).value; var ext = getExt(path); var re = new RegExp("(^|\\s|,)" + ext + "($|\\s|,)", "ig"); if(extensions != '' && (re.exec(extensions) == null || ext == '')) { $.messager.alert("对不起,只能上传jpg, jpeg, png, gif类型的图片!"); $('#'+id).val(''); return false; } //showLoading(); return true; } function getExt(path) { return path.lastIndexOf('.') == -1 ? '' : path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase(); }