javascript - Create repeating form section, remove field values -
i have form repeating section. create repeating section, using slight variant of code:
// add new repeating section var attrs = ['for', 'id', 'name']; function resetattributenames(section) { var tags = section.find('input, label'), idx = section.index(); tags.each(function() { var $this = $(this); $.each(attrs, function(i, attr) { var attr_val = $this.attr(attr); if (attr_val) { $this.attr(attr, attr_val.replace(/_\d+$/, '_'+(idx + 1))) } }) }) } $('.addfight').click(function(e){ e.preventdefault(); var lastrepeatinggroup = $('.repeatingsection').last(); var cloned = lastrepeatinggroup.clone(true) cloned.insertafter(lastrepeatinggroup); resetattributenames(cloned) }); // delete repeating section $('.deletefight').click(function(e){ e.preventdefault(); var current_fight = $(this).parent('div'); var other_fights = current_fight.siblings('.repeatingsection'); if (other_fights.length === 0) { alert("you should atleast have 1 fight"); return; } current_fight.slideup('slow', function() { current_fight.remove(); // reset fight indexes other_fights.each(function() { resetattributenames($(this)); }) }) }); jsfiddle of - http://jsfiddle.net/unfxn/27/
i found code thread: repeating div form fields
however, there problem code: if fill in form fields, , click "add fight," new repeated section not duplicates form fields, values. think need use clear values of new form section:
$(this).closest('form').find("input[type=text], textarea").val(""); but i'm not sure how incorporate functions have. how can clear value of fields once new form section has been created?
you can remove values input fields in newly cloned:
cloned.find("input").val(""); edit:
to reset radio group can set checked value false:
cloned.find("input:radio").attr("checked", false);
Comments
Post a Comment