|
1 | 1 | /* |
2 | | - * jQuery Repeatable Fields v0.2 |
| 2 | + * jQuery Repeatable Fields v1.0 |
3 | 3 | * http://www.rhyzz.com/repeatable-fields.html |
4 | 4 | * |
5 | 5 | * Copyright (c) 2013 Rhyzz |
|
26 | 26 | var settings = $.extend(default_settings, custom_settings); |
27 | 27 |
|
28 | 28 | // Initialize all repeatable field wrappers |
29 | | - |
30 | 29 | initialize(this); |
31 | 30 |
|
32 | 31 | function initialize(parent) { |
|
35 | 34 |
|
36 | 35 | var container = $(wrapper).children(settings.container); |
37 | 36 |
|
| 37 | +// Disable all form elements inside the row template |
| 38 | +$(container).children(settings.template).hide().find(':input').each(function() { |
| 39 | +jQuery(this).prop('disabled', true); |
| 40 | +}); |
| 41 | + |
38 | 42 | $(wrapper).on('click', settings.add, function(event) { |
39 | 43 | event.stopImmediatePropagation(); |
40 | 44 |
|
41 | | -var row_template = $(container).children(settings.template).html(); |
| 45 | +var row_template = $($(container).children(settings.template).clone().removeClass(settings.template.replace('.', ''))[0].outerHTML); |
42 | 46 |
|
43 | | -if($(container).children(settings.template).attr('type') == 'application/json') { |
44 | | -var row_template = JSON.parse(row_template); |
45 | | -} |
| 47 | +// Enable all form elements inside the row template |
| 48 | +jQuery(row_template).find(':input').each(function() { |
| 49 | +jQuery(this).prop('disabled', false); |
| 50 | +}); |
46 | 51 |
|
47 | 52 | if(typeof settings.before_add === 'function') { |
48 | 53 | settings.before_add(container); |
|
55 | 60 | } |
56 | 61 |
|
57 | 62 | // The new row might have it's own repeatable field wrappers so initialize them too |
58 | | - |
59 | 63 | initialize(new_row); |
60 | 64 | }); |
61 | 65 |
|
|
84 | 88 | }); |
85 | 89 | } |
86 | 90 |
|
87 | | -/* |
88 | | -TODO: |
89 | | -This function doesn't take into consideration multiple references to {{row-count-placeholder}}'s so this might not work with nested |
90 | | -repeatable field wrappers |
91 | | -*/ |
92 | | - |
93 | 91 | function after_add(container, new_row) { |
94 | | -var row_count = $(container).children(settings.row).length; |
| 92 | +var row_count = $(container).children(settings.row).filter(function() { |
| 93 | +return !jQuery(this).hasClass(settings.template.replace('.', '')); |
| 94 | +}).length; |
95 | 95 |
|
96 | 96 | $('*', new_row).each(function() { |
97 | 97 | $.each(this.attributes, function(index, element) { |
|
0 commit comments