简体   繁体   English

如何只使Dropzone.js预览Div可点击而不是整个表格

[英]How to make only the Dropzone.js Previews Div clickable and not the whole form

I have to use dropzone.js form, which sends a couple of inputs and a file upload info to the other page. 我必须使用dropzone.js表单,它将几个输入和一个文件上传信息发送到另一个页面。

My dropzone code looks like this -- > 我的dropzone代码看起来像这样 - >

Dropzone.options.mydropzone = {
  maxFiles: 1,
  maxFilesize: 10, //mb
  acceptedFiles: 'image/*',
  addRemoveLinks: true,
  autoProcessQueue: false,// used for stopping auto processing uploads
  autoDiscover: false,
  paramName: 'prod_pic',
  previewsContainer: '#dropzonePreview', //used for specifying the previews div
  clickable: false, //used this but now i cannot click on previews div to showup the file select dialog box

  accept: function(file, done) {
    console.log("uploaded");
    done();
   //used for enabling the submit button if file exist 
    $( "#submitbtn" ).prop( "disabled", false );
  },

  init: function() {
    this.on("maxfilesexceeded", function(file){
        alert("No more files please!Only One image file accepted.");
        this.removeFile(file);
    });
      var myDropzone = this;
    $("#submitbtn").on('click',function(e) {
       e.preventDefault();
       myDropzone.processQueue();

    });

       this.on("reset", function (file) {   
              //used for disabling the submit button if no file exist 
              $( "#submitbtn" ).prop( "disabled", true );
        });

  }

};

But I want to make only the Previews container both Clickable and Drag and Drop ,which I have set by using previewsContainer: '#dropzonePreview' , but not the whole form . 但是我想只使用Previews容器同时使用previewsContainer: '#dropzonePreview'来设置Clickable和Drag and Drop, 而不是整个表单

If I use clickable:false I wont be able to click on previews div to show the file upload dialog box and also even if I drag n drop the file anywhere on the form it takes it. 如果我使用clickable:false我将无法单击预览div来显示文件上传对话框,即使我将文件拖放到表单上的任何位置也是如此。 I dont want that to happen I just want the Previews container to be drag n drop AND Clickable but at the same time if I click on submit the whole form must get uploaded. 我不希望发生这种情况我只想让Previews容器拖放和可点击但同时如果我点击提交,则必须上传整个表单。

I have read this dropzone tutorial Combine normal form with Dropzone but thats just the half of what i actually want to do. 我已经阅读了这个dropzone教程将普通形式与Dropzone相结合,但这只是我实际想要做的事情的一半。

Is there any way we can achieve all of this using Dropzone efficiently ?. 有没有什么方法可以有效地使用Dropzone实现所有这些?

I have been working on this as well and finally stumbled across the answer on the bootstrap page. 我一直在研究这个问题,最后偶然发现了引导页面上的答案。

The key is setting the clickable: option to wherever you want your active Dropzone area to be. 关键是将clickable:选项设置为您希望活动Dropzone区域的位置。 Using your example, if you want your preview area to also be your drop/click area, set clickable:'#dropzonePreview', and that will make that area active. 使用您的示例,如果您希望预览区域也是您的下拉/点击区域,请设置clickable:'#dropzonePreview',这将使该区域处于活动状态。 If you want the "Drop Files" image displayed in there as well use this: 如果你想在那里显示“Drop Files”图像,请使用:

<div id="dropzonePreview" class="dz-default dz-message">
  <span>Drop files here to upload</span>
</div>

This allows you to use a single Dropzone form (thus all fields get submitted as one) while still allowing you to have a smaller area designated for dropping/clicking. 这允许您使用单个Dropzone表单(因此所有字段都作为一个提交),同时仍允许您指定用于删除/单击的较小区域。

One note though, don't make it too small, as if you drag and drop outside the area it loads the image in the browser in place of the page. 但需要注意的是,不要太小,就像拖放到区域之外一样,它会在浏览器中加载图像而不是页面。

Alternatively you can create dropzones programmaticaly (even on non form elements) by instantiating the Dropzone class http://www.dropzonejs.com/#toc_4 或者,您可以通过实例化Dropzone类来创建dropmatic的programmaticaly(甚至在非表单元素上) http://www.dropzonejs.com/#toc_4

You need to add the dz-clickable class to your desired element. 您需要将dz可点击类添加到所需元素。

HTML HTML

<div class="dropzone dz-clickable" id="myDrop">
    <div class="dz-default dz-message" data-dz-message="">
        <span>Drop files here to upload</span>
    </div>
</div>

JavaScript JavaScript的

// Dropzone class:
var myDropzone = new Dropzone("div#myDrop", { url: "/file/post"});

// If you use jQuery, you can use the jQuery plugin Dropzone ships with:
$("div#myDrop").dropzone({ url: "/file/post" });

Note 注意

If you receive a console error saying: Dropzone already attached , make sure to add this line before initiating your new Dropzone object. 如果您收到控制台错误消息: Dropzone already attached ,请确保在启动新的Dropzone对象之前添加此行。

Dropzone.autoDiscover = false;

The documentation says to set the option: "clickable: true", but... 文档说要设置选项:“clickable:true”,但......

My problem was I had added visible markup within the upload form (box). 我的问题是我在上传表单(框)中添加了可见标记。 If you want every point in the box to be clickable, you can't include any other visible markup in your view, you need to add it to the option "dictDefaultMessage." 如果您希望框中的每个点都可以单击,则不能在视图中包含任何其他可见标记,您需要将其添加到“dictDefaultMessage”选项中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM