我有一个图片上传按钮,当从浏览窗口中选择时会自动上传文件。
尽管如此,它第二次不起作用,那是因为未触发 onChange 事件。为什么呢?
这是一个用例:
1. 单击按钮时,我调用uploadMessagePicture(),它会打开浏览窗口。
2. 当用户选择了一张图片时,会检测到一个 onChange 事件,该事件会触发 ajaxFileUpload() 并显示照片编辑器区域。
3. 上传完成后,会显示图片预览。4. 用户通过调用 deleteMessageImage() 删除图片(同时清除字段并隐藏照片编辑器)。
5. 用户尝试上传另一张图片。
此时会出现浏览窗口,但是当我选择图片时,不会触发 onChange 事件,因此什么也没有发生……(而且,文件名似乎甚至没有传输到输入的值字段。)
我在 Firefox 23.0.1
这是HTML:
<a class="im-photo-btn" href="javascript:;" onclick="javascript:uploadMessagePicture();"><i class="icon-join-photo"></i></a>
<div class="editor-photoarea" id="editor-photoarea" style="display:none;">
<input name="image_message_file" type="hidden" id="image_message_file" value="" />
<div id="image_message_upload">
<input name="image-message" type="file" id="image-message" style="display:none; visibility:hidden;" />
<!-- hide it and trigger it from the photo btn (need both display:none + visibility:hiden for browser compatibility) -->
</div>
<div class="loading" id="image_message_loading" style="display:none;"><img alt="<?php echo $lang["Loading"];?>" src="lib/immedia/img/ajax-loader.gif" /> <?php echo $lang["Loading"];?></div>
<div class="preview" style="display:none;" id="image_message_preview">
<!-- <div>Image preview :</div>
<div id='small_message_msg'></div>
<img src='' />
<div class='btnoptions'>
<a onclick='javascript:deleteMessageImage();' href='javascript:;' class='button' title="Delete photo">
<span class='ui-icon ui-icon-closethick smsg'></span>Delete
</a>
</div>-->
</div>
这是自动上传图片的javascript
$(document).ready(function (){
$("#image-message").on('change', function () {
$('#editor-photoarea').show();
ajaxFileUploadMessagePicture();
});
});
function uploadMessagePicture(){
//trigger the browse click
if($('#image_message_file').val() == ''){
//let the person upload a file only if none are there
$('#image-message').trigger('click');
}
}
function ajaxFileUploadMessagePicture(){
$("#msg_error_no_image_message").hide();
var imageName = $("#image-message").val();
if(imageName != ''){
$("#image_message_loading").show();
$('#sendBtn').attr('disabled', 'disabled');
$.ajaxFileUpload
(
{
url:siteRegionDir+'lib/immedia/uploadMessagePicture.php',
secureuri:false,
fileElementId:'image-message',
dataType: 'json',
success: function (data, status)
{
updateMessagePicture(data);
},
error: function (data, status, e)
{
alert(e);
}
}
)
}else{
$("#msg_error_no_image_message").show();
}
return false;
}
这是删除图片并清除字段的Javascript
function deleteMessageImage(){
//delete the image
var file = $("#image_message_file").val();
if(file != '') {
$.post(siteRegionDir+'lib/immedia/deleteMessagePicture.php',{filename:file}, function(data) {
clearPictureAttachment();
$('#editor-photoarea').hide();//make sure it is hidden
},"html"
);
}else{
clearPictureAttachment();
$('#editor-photoarea').hide();//make sure it is hidden
}
}
function clearPictureAttachment(){
//var control = $("#image-message");
$("#image-message").attr('value', '');
//control.replaceWith( control = control.clone( true ) );//so it resets it all, truw = keep the bound events
$("#image_message_file").attr('value', '');
$("#image_message_loading").hide();
$("#image_message_upload").show();
$("#image_message_preview").hide();
}
任何帮助都会很棒!谢谢!