javascript - jQuery Ajax 上传和 ID

标签 javascript php jquery ajax

你好,我得到了这个代码:

$(document).ready(function(){

$('#upload-file-selector').change(function(){

     var $currID = $("#upload-file-selector").attr('data-uid')

    $(this).simpleUpload("uploadFile.php", {

        start: function(file){
            //upload started
            $('#filenameD').html(file.name);
            $('#progressD').html("");
            $('#progressBarD').width(0);
        },

        progress: function(progress){
            //received progress
            $('#progressD').html("Progress: " + Math.round(progress) + "%");
            $('#progressBarD').width(progress + "%");
        },

        success: function(data){
            //upload successful
            $('#progressD').html("Success!<br>Data: " + JSON.stringify(data));
        },

        error: function(error){
            //upload failed
            $('#progressD').html("Failure!<br>" + error.name + ": " + error.message);
        }

    });

});

});

它适用于这个插件: SimpleUpload

如你所见,我有 var currID。如何将此 ID 与文件一起发送到 PHP?如何将 var 绑定(bind)到 JSON 字符串?

最佳答案

如果您想通过 POST 传递 $currID,请使用 SimpleUpload 的 data 选项。

http://simpleupload.michaelcbrook.com/#settings

data object - A set of key-value pairs containing the POST data you would like to send to the server along with each file. This is not automatically populated by any surrounding forms.

使用您的代码:

$(document).ready(function(){
  $('#upload-file-selector').change(function(){;
    var $currID = $("#upload-file-selector").attr('data-uid')
    $(this).simpleUpload("uploadFile.php", {
      data: {
        "id": $currID
      },
      start: function(file){
        //upload started
        $('#filenameD').html(file.name);
        $('#progressD').html("");
        $('#progressBarD').width(0);
      },
      progress: function(progress){
        //received progress
        $('#progressD').html("Progress: " + Math.round(progress) + "%");
        $('#progressBarD').width(progress + "%");
      },
      success: function(data){
        //upload successful
        $('#progressD').html("Success!<br>Data: " + JSON.stringify(data));
      },
      error: function(error){
        //upload failed
        $('#progressD').html("Failure!<br>" + error.name + ": " + error.message);
      }
    });
  });
});

关于javascript - jQuery Ajax 上传和 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41622552/

相关文章:

javascript - 自动滑动照片 jQuery 图像 slider

javascript - 如何将第二个参数传递给地理定位成功函数?

Javascript 表单验证仅适用于 firefox

带有分号插入的 Javascript 缩小

javascript - 使用 JavaScript Fetch 和 POST 发送表单数据,然后在 PHP 中处理

php - 在 PHP 中使用变量作为函数名

javascript - 如何在 JavaScript 中使字符串的第一个字母大写?

php - 超薄框架 session

javascript - 使 onclick 对所有列表元素起作用

javascript - 使用angular打开页面时如何展开折叠?