想用ajax异步方式上传头像,实现修改头像的功能,但是用普通的jquery ajax不管用。要用到jquery的插件。
引入的js
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="js/usercenter.js"></script>
usercenter.js 界面部分代码:
/*更换头像ajax
* */
$("#uploadImg").click(function(){
if($("#userImg").val()==""){
alert("请选择图片上传");
return;
}
$("#UpLoadForm").ajaxSubmit({
type:"POST",
url:"userJsonModifyImg",
success: function(data){
json = $.parseJSON(data);
$("#img").attr({ src: "img/"+json.userimg+"",alt: "我的头像" });
}
});
});
更换头像的jsp界面部分代码:
<div role="tabpanel" class="tab-pane" id="change"> <div id="iyoutingche-center-left"> <span>当前头像</span> <img alt="我的头像" src="img/<%=user.getUser_img() %>" class="thumbnail" width="80px" height="80px" id="img"> </div> <div id="iyoutingche-border"></div> <div id="iyoutingche-center-right"> <form id="UpLoadForm" method="post" enctype="multipart/form-data"> <span>更换头像</span><p></p> <input type="file" name="userImg" id="userImg"> <input class="btn btn-primary" type="button" value="上传" id="uploadImg"> </form> </div> </div>
原文链接:https://www.f2er.com/ajax/163670.html