我有一个快速问题要问你们。有人递给我一套潜在客户开发页面,让我把它们启动并运行。形式很棒,除了一个小问题。。。他们使用下面的jQuery允许用户通过单击"添加另一项"按钮提交数据集的多个实例。问题是重复的项目完全重复。相同的名称、id等。显然,当试图通过PHP处理数据时,这是不起作用的,因为只使用了第一个集合。
我仍在学习jQuery,所以我希望有人能为我指明正确的方向,告诉我如何修改下面的插件,在分配的ID和名称的末尾为每个重复的字段分配一个增量整数。因此,每个数据集中的字段是Role、Description、Age。每个附加的数据集将使用ID&fieldname#的name语法,其中#表示数字加1。
提前感谢您的建议!
/** https://github.com/ReallyGood/jQuery.duplicate */
$.duplicate = function(){
var body = $('body');
body.off('duplicate');
var templates = {};
var settings = {};
var init = function(){
$('[data-duplicate]').each(function(){
var name = $(this).data('duplicate');
var template = $('<div>').html( $(this).clone(true) ).html();
var options = {};
var min = +$(this).data('duplicate-min');
options.minimum = isNaN(min) ? 1 : min;
options.maximum = +$(this).data('duplicate-max') || Infinity;
options.parent = $(this).parent();
settings[name] = options;
templates[name] = template;
});
body.on('click.duplicate', '[data-duplicate-add]', add);
body.on('click.duplicate', '[data-duplicate-remove]', remove);
};
function add(){
var targetName = $(this).data('duplicate-add');
var selector = $('[data-duplicate=' + targetName + ']');
var target = $(selector).last();
if(!target.length) target = $(settings[targetName].parent);
var newElement = $(templates[targetName]).clone(true);
if($(selector).length >= settings[targetName].maximum) {
$(this).trigger('duplicate.error');
return;
}
target.after(newElement);
$(this).trigger('duplicate.add');
}
function remove(){
var targetName = $(this).data('duplicate-remove');
var selector = '[data-duplicate=' + targetName + ']';
var target = $(this).closest(selector);
if(!target.length) target = $(this).siblings(selector).eq(0);
if(!target.length) target = $(selector).last();
if($(selector).length <= settings[targetName].minimum) {
$(this).trigger('duplicate.error');
return;
}
target.remove();
$(this).trigger('duplicate.remove');
}
$(init);
};
$.duplicate();
将[]
添加到输入字段的NAME属性的末尾,例如:
<input type ="text" name="name[]"
通过这种方式,$POST['name']
将保存一个字符串数组。对于该元素。它将是一个数组,其键是从0到它所包含的项目数的数字。