display 隐藏上传按钮_文件上传后如何隐藏上传按钮?

I'm using blueimp and jquery UI for file uploads.

I'd like to hide this button once a file is uploaded and display it once again if the photo is removed. How can I go about doing this?

Here is my HTML:

method="POST" enctype="multipart/form-data">

Add one photo...

aria-valuemin="0"

aria-valuemax="100">

The X-TMPL:

{% for (var i=0, file; file=o.files[i]; i++) { %}

{%=file.name%}

Processing...

{% if (!i && !o.options.autoUpload) { %}

Start

{% } %}

{% if (!i) { %}

Remove Photo

{% } %}

{% } %}

{% for (var i=0, file; file=o.files[i]; i++) { %}

{% if (file.thumbnailUrl) { %}

{% } else { %}

{% } %}

{% if (file.url) { %}

{%=file.name%}

{% } else { %}

{%=file.name%}

{% } %}

{% if (file.error) { %}

Error {%=file.error%}

{% } %}

{%=o.formatFileSize(file.size)%}

{% if (file.deleteUrl) { %}

Remove Photo

{% } else { %}

Remove Photo

{% } %}

{% } %}

The closest post I've found on SO was not exactly relevant.

Update:

Tried the callback solution as answered by @ZakariaAcharki:

console.log('start')

$('input[name="image"]')

.bind('fileuploadcompleted', function (e, data) {

console.log('hiding')

$('.fileinput-button').hide();

})

.bind('fileuploaddestroyed', function (e, data) {

console.log('showing')

$('.fileinput-button').show();

});

console.log('ended')

Output reads: 'start' and 'ended'. Not sure why it's not getting fired.

Update 2:

Hiding seems to work with this, but it doesn't show.

$('.fileupload')

.bind('fileuploaddone', function (e, data) {

console.log('hide');

$('.fileinput-button').hide();

})

.bind('fileuploaddestroy', function (e, data) { //tried fileuploaddestroyed too

console.log('show');

$('.fileinput-button').show();

});

The Javascript (aside from the unchanged tmpl.min.js and jquery file upload/UI files):

/*

* jQuery File Upload Plugin JS Example 8.9.1

* https://github.com/blueimp/jQuery-File-Upload

*

* Copyright 2010, Sebastian Tschan

* https://blueimp.net

*

* Licensed under the MIT license:

* http://www.opensource.org/licenses/MIT

*/

/* global $, window */

$(function () {

'use strict';

var uploadPaths = ["fileA", "fileB", "fileC", "fileCA", "fileCB", "fileCC"];

// Initialize the jQuery File Upload widget:

$('.fileupload').each(function (index) {

$(this).fileupload({

dropZone: $(this),

acceptFileTypes: /(\.|\/)(gif|jpe?g|png|doc|docx|pdf|ppt|pptx)$/i, maxFileSize: 10000000, // 10 MB

// Error and info messages:

messages: {

acceptFileTypes: 'Sorry, this file type not allowed. Please make sure the extension of the file is either .gif, .jpg, .jpeg, .png, .doc, .docx, .pdf, .ppt, or .pptx.',

maxFileSize: 'Please make sure your file is under 10 MB in size.'

}

});

// Load existing files:

$(this).addClass('fileupload-processing');

$.ajax({

// Uncomment the following to send cross-domain cookies:

//xhrFields: {withCredentials: true},

url: '/' + uploadPaths[index],

context: $(this)

}).done(function (data) {

$(this).fileupload('option', 'done').call(this, $.Event('done'), {result: {files: data.files}});

$(this).removeClass('fileupload-processing');

});

});

// Enable iframe cross-domain access via redirect option:

$('#fileupload').fileupload(

'option',

'redirect',

window.location.href.replace(

/\/[^\/]*$/,

'/cors/result.html?%s'

)

);

});

解决方案

Here is the solution our developer presented, by using CSS to hide and show the button, binding to the fileuploaddone event.

In the block with: $('.fileupload').each(function (index), bind as needed:

$(this).bind('fileuploaddone', function (e, data){

var form = $(this).closest("form");

var uploadButton = form.find(".btn.btn-info.fileinput-button");

uploadButton.css("visibility", "hidden");

});

// Load existing files:

if (typeof allFiles[index] !== 'undefined')

if (typeof allFiles[index] !== 'undefined'){

$(this).fileupload('option', 'done').call(this, $.Event('done'), {result: {files: allFiles[index].files}});

var form = $(this).closest("form");

var uploadButton = form.find(".btn.btn-info.fileinput-button");

uploadButton.css("visibility", "hidden");

}

});

});

and then later, make the button visible in a markImageAsDeleted function:

var uploadButton = form.find(".btn.btn-info.fileinput-button");

uploadButton.css("visibility", "visible");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值