我使用Jquery表单进行Ajax图像上传。
我的相关HTML:
<div class="input_con imageupload_con">
<form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm">
<a class="button">BROWSE</a>
<input name="ImageFile" id="imageInput" type="file" style="display:none" />
<input type="submit" id="submit-btn" value="Upload" style="display:none" />
<img src="assets/img/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/>
</form>
<div id="output"></div>
My relevant JS:
$(document).ready(function() {
// Simulate a click on the file input button to show the file browser dialog ...
$('#MyUploadForm .button').click(function() {
$('#imageInput').click();
});
// ... and after having selected a image, trigger the form action
$('#imageInput').change(function() {
$('#MyUploadForm').submit();
});
var options = {
target : '#output', // 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
};
$('#MyUploadForm').submit(function() {
$(this).ajaxSubmit(options);
// always return false to prevent standard browser submit and page navigation
return false;
});
$('.dismiss').click(function(e) {
e.preventDefault(e);
$('#output').empty();
$('#MyUploadForm .button').show();
});
}); /* end of document ready */
function afterSuccess() {
// ....
}
function beforeSubmit() {
// ...
}
//function to format bites bit.ly/19yoIPO
function bytesToSize(bytes) {
// ...
}
</script>
我的PHP文件" processsupload . PHP "的输出部分:
//...
echo '<img src="uploads/'.$NewImageName.'" alt="Thumbnail" width="100%" height="auto">';
echo '<a href="#" class="button dismiss">dismiss</a>';
echo '<input type="hidden" id="path_thumbnail" value="uploads/'.$ThumbPrefix.$NewImageName.'">';
echo '<input type="hidden" id="path_resizedimage" value="uploads/'.$NewImageName.'">';
//...
工作正常:上传图像后,PHP文件的echo部分中所述的所有内容都显示在页面上。
让我惊讶的是:被回显的元素并没有显示在源代码中。
根据上面给出的代码,有人知道为什么代码没有显示在源代码中,而是显示出来了吗?
编辑:
我想知道为什么回声元素不显示在源代码中的原因是,单击解散按钮不起作用。
见上文:
$('.dismiss').click(function(e) {
e.preventDefault(e);
$('#output').empty();
$('#MyUploadForm .button').show();
});
当我点击它时,窗口向上滚动到窗口顶部,而不是取消图片....
发生的事情(根据注释)是,当您初始化页面时,还没有类"解散"的任何对象,因此处理程序不会附加。
然后AJAX注入按钮,但此时为时已晚。
需要使用动态(委托)绑定:
$('body').on('click', '.dismiss', function(e) {
...