简体   繁体   中英

Passing variable to jquery upload function

I am new to web programming and I am trying to create a page where I can upload files. I currently have one upload button on my page which works fine, however, I would like two buttons, but using the same function.

I am trying to pass a variable(example in the code as comesfrom= tag or the logouploaddiv) to the function that will let me see that it came from a different button, so I can pass it to the PHP script via the dataType field in the jQuery below I can therefore handle the files coming from each button differently.

I have tried currently with $(this).attr("id") but this is clearly incorrect and wouldn't work anyway. Is there a way I can pass a custom tag to use and read within the function so I can see which button is being used to upload each file at the PHP end or would I just need to duplicate the $('#fileupload').fileupload code with a different ID for my other button? Thanks for reading!

<div id="logouploaddiv"><input class="btn btn-default" id="fileupload" type="file" name="files[]" comesfrom="logo_upload"></div>

<div id="progress" class="progress">
    <div class="progress-bar progress-bar-success"></div>
</div>
<script>
$(function () {
       'use strict';

         var url = window.location.hostname === '' ?
                     '' : 'server/php';
$('#fileupload').fileupload({
    url: url,
    formData: {type: $(this).attr("id")},
    dataType: 'json',
    done: function (e, data) {
        location.reload();
    },
    progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .progress-bar').css(
            'width',
            progress + '%'
        );
    }
}).prop('disabled', !$.support.fileInput)
    .parent().addClass($.support.fileInput ? undefined : 'disabled');
 });

If I understood you correctly, you need one handler for two input:file tags. Try to use this jquery selector:

$("input:file").fileupload({your handler here});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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