javascript - jQuery: Duplicating file input groups -
javascript - jQuery: Duplicating file input groups -
i'm trying utilize jquery duplicate file input groups, when user selects file upload, automatially creates file upload field , description field.
this works fine, except when alter file of file i've selected input for. when that, duplicates file input groups.
any insight why going on?
jsfiddle: http://jsfiddle.net/czlmbjd6/4/
html:
<div id = "add-photos"> <input type="file"></input> <label>description:</label> <input type = "text"></input> </div> <div id = "additional-photos"> </div>
js:
$(document).ready(function(){ bindfileinputs(); }); function bindfileinputs(){ $("input:file").change(function (){ addphotofield(); }); } function addphotofield() { var id = 1; //standin now, dynamically update number later createphotofield(id); } function createphotofield(id){ var p = $("#add-photos"); p.clone().appendto("#additional-photos").removeattr("id").children().find('input,select').each(function(){ $(this).val(''); if ($(this).attr('type') == 'file'){ console.log("type file"); $(this).attr('name', 'data[image][' + id + '][image]'); } if ($(this).attr('type') == 'text'){ console.log("type text"); $(this).attr('name', 'data[image][' + id + '][description]'); } }); bindfileinputs(); }
try
class="snippet-code-js lang-js prettyprint-override">$(document).ready(function() { bindfileinputs($('#add-photos input[type="file"]')); }); function bindfileinputs(input) { //use one() register handler fired 1 time input.one('change', function() { addphotofield(); }); } function addphotofield() { var id = 1; //standin now, dynamically update number later createphotofield(id); } function createphotofield(id) { var p = $("#add-photos"); var $clone = p.clone().appendto("#additional-photos").removeattr("id"); $clone.find('input,select').val(''); $clone.find('input:text').attr('name', 'data[image][' + id + '][description]'); var $file = $clone.find('input:file').attr('name', 'data[image][' + id + '][image]'); bindfileinputs($file) }
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> select file file upload control. should create new file input group. <div id="add-photos"> <input type="file" /> <label>description:</label> <input type="text" /> </div> <div id="additional-photos"></div>
javascript jquery html
Comments
Post a Comment