Jquery dynamic form plugin, trying to rename field labels as I add them
I'm currently using the jquery dynamic form plugin to add form fields. http://sroucheray.org/blog/jquery-dynamic-form/
My challenge is trying to rename the field labels as开发者_运维问答 they are added/cloned:
For example:
Phone Field
Phone Field 2
Phone Field 3
If anyone has experience with this plugin, I would definitely appreciate the assistance. Thanks!
It looks like the for attribute of the labels created are in the format phone#
where #
is the 0 based index,you could do something like this:
var i = 0;
var $label = $('label[for="phone' + i + '"]');
do
{
var txt = 'Phone Field';
if(i > 0)
txt += ' ' + i;
$label.text(txt);
i++;
$label = $('label[for="phone' + i + '"]');
} while($label.length);
Working jsFiddle example here
Edit: In response to David's comment:
One way is to hook when a new clone is added, keeping a count of how many are added (for use in the text). Then you simply rename the added label:
var numLabels = 0;
$(document).ready(function() {
$("#duplicate").dynamicForm("#plus", "#minus", { //options
limit: 3,
afterClone: function(clone) {
numLabels++;
var txt = 'Phone Field ' + numLabels;
$('label', clone).text(txt);
}
});
});
You should look into the .delegate() method of jQuery...If you need help implementing it, let me know.
精彩评论