1.首先HTML代码
<div class="form-group"> <label class="col-sm-2 control-label">图片</label> <div class="col-sm-4"> <input type="hidden" name="image" id="img_url" value="<?php echo $rows['image']; ?>"> <div id="file"> <span>更新图片</span> <input type="file" onchange="showImg(this)" id="uploadForm"> </div> </div> <div class="col-sm-2"> <div class="box"> <section id="t_img"> <img src="<?php echo $rows['image']; ?>"> </section> </div> </div> </div>
jQuery代码
function doUpload(){ if(img_l == ''){ }else{ var formData = new FormData($( "#formxx" )[0]); formData.append('image', img_l); formData.append('id', $('#p_id').val()); $.ajax({ url: 'index.php?m=admin&c=consult&f=ajax_up_img' , type: 'post', data: formData, dataType: "json", async: false, cache: false, contentType: false, processData: false, success:function(data,status){ $('#img_url').attr('value',data['img_url']); //alert(data['img_url']); }, error:function (returndata){ alert(returndata); } }); } return true; }