我有一对输入字段(链接标题,链接报告),用户可以复制它们,以包含提交表单时创建的记录的多个链接,但是我注意到,尽管这些字段附加了数组名称的计数器,例如reportLink[2]
表单提交正文中没有包含其他链接字段,我想知道这是否与我的前端代码有关,或者此过程是否是后端代码配置的结果。此刻,体内收集的全部是一对磁场。
这是我的字段:
<div class="form-group report-link-container p-2">
<label for="report-link">Link to Reports</label>
<p><i>Please make sure all visible URL fields contain http:// or https://. Otherwise you will receive an error</i></p>
<div class="annotation__footer-report-link-form-container">
<a href="#" class="btn btn-outline-primary setup-add-field mb-2">+ Add Another Report</a>
<br/>
<label for="report-title">Title</label>
<a href="#" class="setup-add-field">Add Another Report</a>
<div class="annotation__footer-report-link-form-container">
<input type="text" name="reportTitle[0]" class="form-control annotation-report-title" value=""placeholder="Title">
<input type="text" name="reportLink[0]" class="form-control annotation-report-link" value="" placeholder="Link">
</div>
</div>
</div>
jQuery,用于附加其他字段和名称计数器:
//Append or remove form fields for report links
var formField = $('.report-link-container');
var i = $('.report-link-container p').size();
var fieldName = $('input.annotation-report-link')[0].name.replace('[0]', '');
var reportTitle = $('input.annotation-report-title')[0].name.replace('[0]', '');
var reportTitlePlaceholder = $('input.annotation-report-title')[0].placeholder;
var fieldPlaceholder = $('input.annotation-report-title')[0].placeholder;
var reportTitleCounter = 1;
var fieldCounter = 1;
$('.setup-add-field').on('click', function() {
$('<div class="annotation__footer-report-link-form-container"><div class="report-link-container"><p class="setup-form-field-wrapper"><a href="#" class="remove">Remove</a><label>Title</label><input type="text" class="form-control annotation-report-title" size="20" name="' + reportTitle + '['+ reportTitleCounter++ +']" value=""' + reportTitlePlaceholder + '" placeholder="Title"/><label>Link</label><input type="text" class="form-control annotation-report-link" size="20" name="' + fieldName + '['+ fieldCounter++ +']" value=""' + fieldPlaceholder + '" placeholder="Link"/></p></div></div>').appendTo(formField);
i++;
return false;
});
$('.report-link-container').on('click', '.remove', function() {
if (i >= 1) {
$(this).closest('.annotation__footer-report-link-form-container').remove();
i--;
}
return false;
});
结果主体,无论是否在表单提交中附加并显示其他字段:
reportTitle[0]
reportLink[0]