我正在使用jQuery文件上傳插件。在這裏,我試圖更新現有的圖像:jQuery文件上傳插件不起作用
$(document).ready(function() {
var options = {
target: '#notification_img', // target element(s) to be updated with server response
beforeSubmit: beforeSubmit, // pre-submit callback
success: afterSuccess, // post-submit callback
resetForm: true // reset the form after successful submit
};
$('#MyImg').submit(function() {
$(this).ajaxSubmit(options);
// always return false to prevent standard browser submit and page navigation
return false;
});
HTML表單
<form action="img_up.php" method="post" enctype="multipart/form-data" id="MyImg">
<label>Upload Photo</label><br>
<input class="inp" id="photo-up" type="file" placeholder="Photo" name="photo-up"/><br/>
<input class="btn" type="submit" id="update-img-btn" value="Update" /><br/>
<img src="images/ajax-loader.gif" id="loading-img" style="display:none; text-align:center;" alt="Please Wait"/>
</form>
我的問題是這樣的頁面重定向到img_up.php
文件,當我提交表單。但是這並不是假設我正在使用AJAX。該圖像沒有上傳到目錄中。我已經包含了所有必需的JavaScript文件。與此
$('#update-img-btn').click(function(e) {
e.preventDefault();
$('#MyImg').ajaxSubmit(options);
// // always return false to prevent standard browser submit and page navigation
return false;
});
...
$('#MyImg').submit(function(event) {
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'img_up.php', // the url where we want to POST
})
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
您在控制檯中遇到什麼錯誤?這一切似乎爲我工作得很好(http://jsfiddle.net/putvande/aqjbLvzr/)你定義'beforeSubmit'和'afterSuccess'? – putvande
是的,我定義了這些功能。此代碼應該工作正常,但不適合我。 –