jquery – 上传文件后如何隐藏上传按钮?

前端之家收集整理的这篇文章主要介绍了jquery – 上传文件后如何隐藏上传按钮?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用blueimp和 jquery UI进行文件上传.

我想在上传文件后隐藏此按钮,如果删除照片则再次显示该按钮.我该怎么做呢?

这是我的HTML:

  1. <form class="fileupload" action="${pageContext.request.contextPath}/someUrl"
  2. method="POST" enctype="multipart/form-data">
  3. <noscript><input type="hidden" name="redirect" value="https://www.somedomain.com"/></noscript>
  4. <input type="hidden" name="type" value="image1">
  5.  
  6. <div class="row fileupload-buttonbar">
  7. <div class="col-lg-7">
  8. <span class="btn btn-info fileinput-button"><i class="fa fa-plus"></i> Add one photo...
  9. <input type="file" name="image" accept="image/png,image/jpeg">
  10. </span>
  11. <span class="fileupload-process"></span>
  12. </div>
  13. <div class="col-lg-5 fileupload-progress fade">
  14. <div class="progress progress-striped active" role="progressbar"
  15. aria-valuemin="0"
  16. aria-valuemax="100">
  17. <div class="progress-bar progress-bar-success" style="width:0;"></div>
  18. </div>
  19. <div class="progress-extended">&nbsp;</div>
  20. </div>
  21. </div>
  22. <table role="presentation" class="table table-striped">
  23. <tbody class="files"></tbody>
  24. </table>
  25. </form>

X-TMPL:

  1. <!-- The template to display files available for upload -->
  2. <script id="template-upload" type="text/x-tmpl">
  3. {% for (var i=0,file; file=o.files[i]; i++) { %}
  4. <tr class="template-upload fade">
  5. <td>
  6. <span class="preview"></span>
  7. </td>
  8. <td>
  9. <p class="name">{%=file.name%}</p>
  10. <strong class="error text-danger"></strong>
  11. </td>
  12. <td>
  13. <p class="size">Processing...</p>
  14. <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
  15. </td>
  16. <td>
  17. {% if (!i && !o.options.autoUpload) { %}
  18. <button class="btn btn-info start" disabled>
  19. <span>Start</span>
  20. <i class="fa fa-caret-right"></i>
  21. </button>
  22. {% } %}
  23. {% if (!i) { %}
  24. <button class="btn btn-warning cancel">
  25. <i class="fa fa-trash-o"></i>
  26. <span>Remove Photo</span>
  27. </button>
  28. {% } %}
  29. </td>
  30. </tr>
  31. {% } %}
  32. </script>
  33. <!-- The template to display files available for download -->
  34. <script id="template-download" type="text/x-tmpl">
  35. {% for (var i=0,file; file=o.files[i]; i++) { %}
  36. <tr class="template-download fade">
  37. <td>
  38. <span class="preview">
  39. {% if (file.thumbnailUrl) { %}
  40. <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
  41. {% } else { %}
  42. <img src="{%=file.thumbnail_url%}">
  43. {% } %}
  44. </span>
  45. </td>
  46. <td>
  47. <p class="name">
  48. {% if (file.url) { %}
  49. <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
  50. {% } else { %}
  51. <span>{%=file.name%}</span>
  52. {% } %}
  53. </p>
  54. {% if (file.error) { %}
  55. <div><span class="text-danger"><i class="fa fa-exclamation-circle"></i> Error</span> {%=file.error%}</div>
  56. {% } %}
  57. </td>
  58. <td>
  59. <span class="size">{%=o.formatFileSize(file.size)%}</span>
  60. </td>
  61. <td>
  62. {% if (file.deleteUrl) { %}
  63. <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
  64. <i class="fa fa-trash-o"></i>
  65. <span>Remove Photo</span>
  66. </button>
  67. <input type="checkBox" name="delete" value="1" class="toggle">
  68. {% } else { %}
  69. <button class="btn btn-warning cancel">
  70. <i class="fa fa-trash-o"></i>
  71. <span>Remove Photo</span>
  72. </button>
  73. {% } %}
  74. </td>
  75. </tr>
  76. {% } %}
  77. </script>

我在SO上最近的帖子found并不完全相关.

更新:

尝试了@ZakariaAcharki回答的回调解决方案:

  1. console.log('start')
  2. $('input[name="image"]')
  3. .bind('fileuploadcompleted',function (e,data) {
  4. console.log('hiding')
  5. $('.fileinput-button').hide();
  6. })
  7. .bind('fileuploaddestroyed',data) {
  8. console.log('showing')
  9. $('.fileinput-button').show();
  10. });
  11. console.log('ended')

输出读数:’开始’和’结束’.不知道为什么它是not getting fired.

更新2:
隐藏似乎与此有关,但它没有显示.

  1. $('.fileupload')
  2. .bind('fileuploaddone',data) {
  3. console.log('hide');
  4. $('.fileinput-button').hide();
  5. })
  6. .bind('fileuploaddestroy',data) { //tried fileuploaddestroyed too
  7. console.log('show');
  8. $('.fileinput-button').show();
  9. });

Javascript(除了未更改的tmpl.min.js和jquery文件上传/ UI文件):

  1. /*
  2. * jQuery File Upload Plugin JS Example 8.9.1
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2010,Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * http://www.opensource.org/licenses/MIT
  10. */
  11.  
  12. /* global $,window */
  13.  
  14. $(function () {
  15. 'use strict';
  16.  
  17. var uploadPaths = ["fileA","fileB","fileC","fileCA","fileCB","fileCC"];
  18.  
  19. // Initialize the jQuery File Upload widget:
  20. $('.fileupload').each(function (index) {
  21. $(this).fileupload({
  22. dropZone: $(this),acceptFileTypes: /(\.|\/)(gif|jpe?g|png|doc|docx|pdf|ppt|pptx)$/i,maxFileSize: 10000000,// 10 MB
  23.  
  24. // Error and info messages:
  25. messages: {
  26. 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.'
  27. }
  28. });
  29.  
  30. // Load existing files:
  31. $(this).addClass('fileupload-processing');
  32. $.ajax({
  33. // Uncomment the following to send cross-domain cookies:
  34. //xhrFields: {withCredentials: true},url: '/' + uploadPaths[index],context: $(this)
  35. }).done(function (data) {
  36. $(this).fileupload('option','done').call(this,$.Event('done'),{result: {files: data.files}});
  37. $(this).removeClass('fileupload-processing');
  38. });
  39. });
  40.  
  41. // Enable iframe cross-domain access via redirect option:
  42. $('#fileupload').fileupload(
  43. 'option','redirect',window.location.href.replace(
  44. /\/[^\/]*$/,'/cors/result.html?%s'
  45. )
  46. );
  47. });

解决方法

你有没有尝试过callbacks fileuploadcompleted和fileuploaddestroyed:
  1. $('input[name="image"]')
  2. .bind('fileuploadcompleted',data) {
  3. $('.fileinput-button').hide();
  4. })
  5. .bind('fileuploaddestroyed',data) {
  6. $('.fileinput-button').show();
  7. });

希望这可以帮助.

猜你在找的jQuery相关文章