页面:
<div class="col-lg-3 h3"> <h3><input style="display: none;" type="file" name="image" class="productImage" data-productId="${image.id}"></h3> <h4 class="h4">重新选择</h4> <img class="imgOn" src="${image.url}" /> </div>js:
<script> $(document).ready(function() { $(".productImage").on('change',function(){ var $this = $(this); var id = $(this).attr("data-productId"); var file = this.files[0]; var formData = new FormData(); formData.append("file", file); formData.append("id", id); $.ajax({ url: "updateProductImg.jss", type: "POST", data: formData, cache: false, contentType: false, processData: false, dataType: "json", success: function(result) { $this.parent("h3").siblings("img").attr("src",result.data); }, error: function() { } }); }); $(".h4").click(function(){ $(".productImage").click(); }); }); </script>