最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How do I call a Javascript function when I click a "Submit" button? - Stack Overflow

programmeradmin6浏览0评论

I am trying to implement Drag and Drop file upload functionality into a webpage. I have this javascript function in a dropzone.js file:

Dropzone.prototype.processFile = function(file) {
  this.filesProcessing.push(file);
  file.processing = true;
  this.emit("processingfile", file);
  return this.uploadFile(file);
};

And I have this in my html file:

<script src="dropzone.js"></script>
<input type="submit" class="btn" value="Submit" />

I downloaded the Dropzone.js file from to implement in my page. The Dropzone has the functionality to either start uploading files as soon as they are dropped onto the page or wait until the user calls the function mentioned above.

How do I call the function when I click the "Submit" button? I'm pretty unfamiliar with how the dropzone thing really works. The instructions from the creator of dropzone.js say I should call "myDropzone.processFile(file);" if I don't want the dropzone to immediately start uploading as soon as files are dropped into the element, but I'm not sure how to call this from my html button.

I am trying to implement Drag and Drop file upload functionality into a webpage. I have this javascript function in a dropzone.js file:

Dropzone.prototype.processFile = function(file) {
  this.filesProcessing.push(file);
  file.processing = true;
  this.emit("processingfile", file);
  return this.uploadFile(file);
};

And I have this in my html file:

<script src="dropzone.js"></script>
<input type="submit" class="btn" value="Submit" />

I downloaded the Dropzone.js file from http://www.dropzonejs. to implement in my page. The Dropzone has the functionality to either start uploading files as soon as they are dropped onto the page or wait until the user calls the function mentioned above.

How do I call the function when I click the "Submit" button? I'm pretty unfamiliar with how the dropzone thing really works. The instructions from the creator of dropzone.js say I should call "myDropzone.processFile(file);" if I don't want the dropzone to immediately start uploading as soon as files are dropped into the element, but I'm not sure how to call this from my html button.

Share Improve this question edited Aug 13, 2013 at 11:13 Shemeer M Ali 1,04117 silver badges39 bronze badges asked Apr 22, 2013 at 5:43 ahabosahabos 2073 gold badges7 silver badges18 bronze badges 3
  • Did you read the instructions ? it specifically states: "Dropzone does not handle your file uploads on the server. You have to implement the code to receive and store the file yourself." – Nir Alfasi Commented Apr 22, 2013 at 5:55
  • I did read the instructions. If I have the files upload immediately, they get saved to a folder on my local machine for now. What I want to do is save them only after I click the submit button. – ahabos Commented Apr 22, 2013 at 6:10
  • This link addresses the same issue. It disables automatic uploading and manually uploads on click. – anpsmn Commented Apr 22, 2013 at 6:51
Add a ment  | 

5 Answers 5

Reset to default 2
<div id="previews" class="dropzone-previews"></div>


<button id="clickable">Click me to select files</button>

<script>
  new Dropzone(document.body, { // Make the whole body a dropzone
    url: "/upload/url", // Set the url
    previewsContainer: "#previews", // Define the container to display the previews
    clickable: "#clickable" // Define the element that should be used as click trigger to select files.
  });
</script>

Try this out, it worked for me:

<form id="my-dropzone" class="dropzone" action="file-upload.php"></form>
<input type="button" value="Upload Files" onclick="uploadClicked()" />
<script type="text/javascript" src="dropzone.js"></script>
<script type="text/javascript">
    Dropzone.options.myDropzone = {
        maxFilesize: 2, // MB,
        enqueueForUpload: false
    };

    function uploadClicked() {
        var dz = Dropzone.forElement("#my-dropzone");
        for (var i = 0; i < dz.files.length; i++) {
            dz.filesQueue.push(dz.files[i]);
        }
    dz.processQueue();
    }
</script>

Here's a link to a tutorial on the subject: http://bit.ly/1jVOKfd

I found that the sample script in the tutorial worked well for a button embedded in the dropzone (i.e., the form element). If you wish to have the button outside the form element, I was able to acplish it using a click event:

First, the HTML:

      <form id="my-awesome-dropzone" action="/upload" class="dropzone">  
        <div class="dropzone-previews"></div>
        <div class="fallback"> <!-- this is the fallback if JS isn't working -->
        <input name="file" type="file" multiple />
        </div>

      </form>
      <button type="submit" id="submit-all" class="btn btn-primary btn-xs">Upload the file</button>

Then, the script tag....

      Dropzone.options.myAwesomeDropzone = { // The camelized version of the ID of the form element

        // The configuration we've talked about above
        autoProcessQueue: false,
        uploadMultiple: true,
        parallelUploads: 25,
        maxFiles: 25,

        // The setting up of the dropzone
        init: function() {
          var myDropzone = this;

         // Here's the change from enyo's tutorial...

            $("#submit-all").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                myDropzone.processQueue();
                }
            );

        }

      }

There must be some initialization code for dropzone, that might have been called in onLoad event. first disable that and then call the

document.getElementById("btnSubmit").onclick = Dropzone.prototype.processFile ;

<script>
function js_fun()
{
//do manipulations here and return true on success and false on failure 
}
</script>
<form method='get' onsubmit='return js_fun()'>
<input type='submit'/>
</form>
发布评论

评论列表(0)

  1. 暂无评论