JQuery文件上载未检测到动态添加的输入
JQuery file upload not detecting dynamically added inputs
我有一个表单,在这个表单中你可以添加1个或多个用户,每个用户必须上传一个img文件。
我有jquery.fileupload库,它对第一次输入很好,但如果用户需要添加更多字段来添加更多用户,那么fileupload jquery代码不会检测到那些动态添加的输入字段。
字段所在位置的HTML代码:
<div class="input text input-nb">
<!-- DIV where the input fields are adden from jquery -->
<div id="bloque_integrante_wrap">
</div>
<!-- DIV that contains the link to add more input fields -->
<div id="agregar_mas_wrap">
<a id="agregar-mas-integrantes-nb" href="javascript:void(0)"><img id="agregar-mas-nb-img" src="../img/agregar-mas.jpg" /> <label id="agregar-mas-nb-2">agregar mas</label></a>
</div>
</div>
我的js/jquery代码如下:
$(document).ready(function(){
var index = 1;
var bloque = '<div class="bloque_integrante">'+
'<div class="inner_wrap_nb" id="bloque_integrante_'+index+'">'+
'<label>Nombre </label><input class="nombre_integrante_nb" name="data[nombre_integrante_'+index+']" type="text" />'+
'<label>Rol </label><input class="rol_integrante_nb" name="data[rol_integrante_'+index+']" type="text" />'+
'<a class="bonito-button subir-ci-nb fileinput-button">Subir Copia C.I.'+
' <input type="file" name="data[ci_integrante_'+index+']" id="ci_integrante_'+index+'" class="ci_integrante_nb" > </a>'+
'<a class="eliminar-integrante-nb" href="javascript:void(0)"></a>'+
'<img class="nb-ci-prev-status" id="preview_nb_'+index+'" src="../img/check-nb.png">'+
'</div>'+
'</div>';
// Adding the default first block, must always have 1 input at start
$("#bloque_integrante_wrap").append(bloque);
// The listener to when user clicks to add more fields
$("#agregar-mas-integrantes-nb").on("click",function(){
index = index + 1;
var bloque2 = '<div class="bloque_integrante">'+
'<div class="inner_wrap_nb" id="bloque_integrante_'+index+'">'+
'<label>Nombre </label><input class="nombre_integrante_nb" name="data[nombre_integrante_'+index+']" type="text" />'+
'<label>Rol </label><input class="rol_integrante_nb" name="data[rol_integrante_'+index+']" type="text" />'+
'<a class="bonito-button subir-ci-nb fileinput-button">Subir Copia C.I.'+
' <input type="file" name="data[ci_integrante_'+index+']" id="ci_integrante_'+index+'" class="ci_integrante_nb" > </a>'+
'<a class="eliminar-integrante-nb" href="javascript:void(0)"></a>'+
'<img class="nb-ci-prev-status" id="preview_nb_'+index+'" src="../img/check-nb.png">'+
'</div>'+
'</div>';
$("#bloque_integrante_wrap").append(bloque2);
});
// This is where i process when file input is changed (user choses file)
// This only works for the first input, added as soon as document loads
$("#subir-imagen-banda-nb").on("change", function(){
readURL(this,"#foto-nb-icon",30,30);
});
});
函数readURL将上传文件的URL添加到DOM元素,通常是显示上传文件的拇指图标。
这只适用于我在文档加载后添加的第一个元素。
@dave指出,我应该使用事件委派来检测我动态添加的元素。
因此,为了检测这一点,我的Jquery侦听器如下所示:
$("#bloque_integrante_wrap").on("change",".ci_integrante_nb",function(){
var contenedor1 = $(this).attr("id");
var tmp = contenedor1.split("_");
var num = tmp[2];
readURL(this,"#preview_nb_"+num,30,30,"feedback");
});
请注意我是如何使用#bloque_integrante_wrap
div选择器的,它是添加动态元素的地方,而.ci_integrante_nb
是每个动态元素所具有的类。
相关文章:
- 如何在Google柱状图中动态添加行/列
- 使用jQuery动态添加表并在其中动态添加行
- 如何使用Angular动态添加iframe-src
- 如何向JSON数组动态添加属性
- 动态添加通过json创建的选择项
- 使用jQuery根据动态生成的html动态添加html
- 使用.on动态添加jquery/js不知道的html元素
- JS动态添加字段-删除按钮不起作用
- 动态添加AngularJS脚本
- 将表行动态添加到表或tbody元素中
- jQuery-通过AJAX调用动态添加具有值的表单元格
- 将特定属性动态添加到元素中
- 在highcharts.js中向点弹出窗口动态添加文本
- 如何在Angular.js中循环动态添加Fields并获取数据并将其发送到服务器
- 使用jQuery动态添加onClick事件
- 如何在Opencart中动态添加JavaScript
- 剑道UI下载列表:如何动态添加新元素
- 动态添加复选框和输入框 jQuery
- 动态添加哈希标记;t工作
- 将图表动态添加到组合高图表中