Javascript 使用数据通过$.ajax发送的混合输入(文本和文件)

Javascript 使用数据通过$.ajax发送的混合输入(文本和文件),javascript,ajax,object,Javascript,Ajax,Object,我尝试将带有两个输入的数据发送到php代码,其中一个只是文本,另一个是类型文件。第二个字段也用于多个文件选择。下面是html部分的代码: <input type="text" id="padron-documento" class="form-control input-lg" name="padron-documento" placeholder="Padrón" /><br /> <div class="alert alert-i

我尝试将带有两个输入的数据发送到php代码,其中一个只是文本,另一个是类型文件。第二个字段也用于多个文件选择。下面是html部分的代码:

        <input type="text" id="padron-documento" class="form-control input-lg" name="padron-documento" placeholder="Padrón" /><br />
        <div class="alert alert-info">
        <input type="file" id="archivos" name="archivos[]" multiple onChange="seleccionado()" />
        </div>
        <div id="cargados">
        <!-- Here we display the loaded files. -->
        </div>

好的,这是javascript部分的代码:

function seleccionado(){
var archivos = document.getElementById("archivos");//We get the value of the input with id archivos
var archivo = archivos.files; //Here we get the value of input (archivos) into an array

//Teh FormData object allow us to create a form passing to it a key/value to send it, this kind of object already have the multipart/form-data to upload files
var datos = new FormData();

padron = $("#padron-documento").val();
//We don't know how much files the user will upload, we iterate the variable
//and we pass key/value with the method append to the object FormData, we use the index "i"    // to avoid repeated values, if we not use it, it will only have the value of the last iteration
for(i=0; i<archivo.length; i++){
datos.append('archivo'+i,archivo[i]);
}

$.ajax({
url:'subir.php', //Url
type:'POST', //Method
contentType:false, //It must be false
data: {padron,datos}, //We pass the object that we already create with archivos and also the text with id padron
processData:false, //It must be false to avoid jquery processing
cache:false //Avoid to save cache
}).done(function(msg)
{$("#cargados").append(msg); //It will show the files loaded into div "cargados"});}
/*****************************/
函数selectionado(){
var archivos=document.getElementById(“archivos”);//我们用id archivos获取输入的值
var archivo=archivos.files;//这里我们将输入值(archivos)放入数组中
//FormData对象允许我们创建一个表单,向它传递一个键/值来发送它,这种对象已经有了要上传文件的多部分/表单数据
var datos=新的FormData();
padron=$(“#padron documento”).val();
//我们不知道用户将上传多少文件,我们迭代变量
//并且我们用方法append传递key/value到对象FormData,我们使用索引“i”//来避免重复值,如果我们不使用它,它将只具有最后一次迭代的值

对于(i=0;iit可能有助于获得英文注释和完整的html代码(对于完整的html代码,我指的是从
的所有内容)我在这个例子中不使用表单标签
我尝试发送一个包含两个输入的表单,
你说你这样做了…发布更多
html
代码是很有帮助的。无论如何,我使用引导主题将此代码放入选项卡式部分,这是我在
$(“#padron documento”)
中仅有的html代码,您没有具有此id的元素。