jQuery Validation插件不验证动态创建的表单元素 [英] jQuery Validation plugin doesn't validate dynamically created form elements
问题描述
我有一个表单,您可以在其中动态添加带有输入元素的新行. 在提交我的表单之前,可以通过使用 http://jqueryvalidation.org/上的插件对它进行验证. 当前,用于添加带有输入元素的新行的代码如下:
function addTimeRow(table, stime)
{
//var rowIdx = $('#seminarTimes tr').length - 1;
var $id = $('<input type="hidden" name="stid[]">');
var $dateField = $('<input type="text" name="date[]" class="input-mini">');
var $date = $('<div class="input-append date">')
.append($dateField)
.append('<span class="add-on"><i class="icon-calendar">');
var $from = $('<input type="text" name="from[]" class="input-mini"> <span>(hh:mm)</span>');
var $to = $('<input type="text" name="to[]" class="input-mini"> <span>(hh:mm)</span>');
if (typeof(stime) !== 'undefined')
{
$id.attr('value', stime.id);
$dateField.attr('value', stime.date);
$from.attr('value', stime.from);
$to.attr('value', stime.to);
}
else
$id.attr('value', -1);
// Attach new input row.
table
.append($('<tr>')
.append($id)
.append($('<td>')
.append($date))
.append($('<td>')
.append($from))
.append($('<td>')
.append($to))
.append($('<td class="vert">')
.append($('<button class="btn btn-mini btnDelTime"><i class="icon-trash">'))));
// Attach rules.
$dateField.rules('add', { required: true });
$from.rules('add', { required: true });
$to.rules('add', { required: true });
// Create pickers.
$date.datepicker({
language: 'de',
autoclose: true,
todayBtn: true,
todayHighlight: true,
}).on('changeDate', function(e) {
editSeminarFormValidator.element($dateField);
$date.datepicker('hide');
});
}
在我的文档就绪事件中,我像这样初始化JQuery验证插件:
var validator = $('#editSeminarForm').validate({
debug: true,
errorLabelContainer: '#error-label',
wrapper: 'li',
messages: {
price: 'Bitte geben Sie einen Preis ein!'
}
});
现在我的实际问题是,没有任何新的输入字段得到验证. 我知道我正在使用输入数组来更轻松地在服务器端处理表单.这些数组是为什么我的输入字段未得到验证的问题吗?
编辑-我当前的解决方案:
var rowIdx = 0;
function addTimeRow(table, stime)
{
var $id = $($.validator.format('<input type="hidden" id="stid{0}" name="stid[{0}]">', rowIdx));
var $dateField = $($.validator.format('<input type="text" id="date{0}" name="date[{0}]" class="input-mini">', rowIdx));
var $date = $('<div class="input-append date">')
.append($dateField)
.append('<span class="add-on"><i class="icon-calendar">');
var $from = $($.validator.format('<input type="text" id="from{0}" name="from[{0}]" class="input-mini"> <span>(hh:mm)</span>', rowIdx));
var $to = $($.validator.format('<input type="text" id="to{0}" name="to[{0}]" class="input-mini"> <span>(hh:mm)</span>', rowIdx));
if (typeof(stime) !== 'undefined')
{
$id.attr('value', stime.id);
$dateField.attr('value', stime.date);
$from.attr('value', stime.from);
$to.attr('value', stime.to);
}
else
$id.attr('value', -1);
// Attach new input row.
table
.append($('<tr>')
.append($id)
.append($('<td>')
.append($date))
.append($('<td>')
.append($from))
.append($('<td>')
.append($to))
.append($('<td class="vert">')
.append($('<button class="btn btn-mini btnDelTime"><i class="icon-trash">'))));
// Attach rules.
$dateField.rules('add', { required: true });
$from.rules('add', { required: true });
$to.rules('add', { required: true });
// Create pickers.
$date.datepicker({
language: 'de',
autoclose: true,
todayBtn: true,
todayHighlight: true,
}).on('changeDate', function(e) {
editSeminarFormValidator.element($dateField);
$date.datepicker('hide');
});
rowIdx++;
}
像魅力一样工作!
用于向新元素添加规则的逻辑似乎没有什么大不了. 尽管,但您需要使用jQuery 选择器(而不是元素的HTML)将.rules()
方法附加到jQuery 对象.>
类似...
$('#myInputID').rules('add', {...});
或...
$('input[name="myname"]').rules('add', {...});
但是您问题的核心在这里...
var $id = $('<input type="hidden" name="stid[]">');
var $dateField = $('<input type="text" name="date[]" class="input-mini">');
var $date = $('<div class="input-append date">')
.append($dateField)
.append('<span class="add-on"><i class="icon-calendar">');
var $from = $('<input type="text" name="from[]" class="input-mini"> <span>(hh:mm)</span>');
var $to = $('<input type="text" name="to[]" class="input-mini"> <span>(hh:mm)</span>');
是否注意到name
属性?每个新行都将是相同的.
问题在于,除非每个input
元素包含一个 unique name
,否则jQuery Validate插件将无法工作.只需修改您的代码以确保为每个新元素创建一个新的name
.
编辑:索引数组可以与此插件一起正常工作.创建新元素时,只需增加索引即可.
name="from[0]"
,name="from[1]"
,name="from[2]"
等
I've a form where you dynamically can add new rows with input elements. Before submitting my form it gets validated by using the plugin from here http://jqueryvalidation.org/. Currently the code for adding a new row with input elements looks like this:
function addTimeRow(table, stime)
{
//var rowIdx = $('#seminarTimes tr').length - 1;
var $id = $('<input type="hidden" name="stid[]">');
var $dateField = $('<input type="text" name="date[]" class="input-mini">');
var $date = $('<div class="input-append date">')
.append($dateField)
.append('<span class="add-on"><i class="icon-calendar">');
var $from = $('<input type="text" name="from[]" class="input-mini"> <span>(hh:mm)</span>');
var $to = $('<input type="text" name="to[]" class="input-mini"> <span>(hh:mm)</span>');
if (typeof(stime) !== 'undefined')
{
$id.attr('value', stime.id);
$dateField.attr('value', stime.date);
$from.attr('value', stime.from);
$to.attr('value', stime.to);
}
else
$id.attr('value', -1);
// Attach new input row.
table
.append($('<tr>')
.append($id)
.append($('<td>')
.append($date))
.append($('<td>')
.append($from))
.append($('<td>')
.append($to))
.append($('<td class="vert">')
.append($('<button class="btn btn-mini btnDelTime"><i class="icon-trash">'))));
// Attach rules.
$dateField.rules('add', { required: true });
$from.rules('add', { required: true });
$to.rules('add', { required: true });
// Create pickers.
$date.datepicker({
language: 'de',
autoclose: true,
todayBtn: true,
todayHighlight: true,
}).on('changeDate', function(e) {
editSeminarFormValidator.element($dateField);
$date.datepicker('hide');
});
}
In my document ready event I initialize the JQuery validation plugin like so:
var validator = $('#editSeminarForm').validate({
debug: true,
errorLabelContainer: '#error-label',
wrapper: 'li',
messages: {
price: 'Bitte geben Sie einen Preis ein!'
}
});
Now my actual problem is, that none of the new input fields gets validated. I know that I'm using input arrays for easier handling the form on the server-side. Are these arrays the problem why my input fields don't get validated?
EDIT - My current solution:
var rowIdx = 0;
function addTimeRow(table, stime)
{
var $id = $($.validator.format('<input type="hidden" id="stid{0}" name="stid[{0}]">', rowIdx));
var $dateField = $($.validator.format('<input type="text" id="date{0}" name="date[{0}]" class="input-mini">', rowIdx));
var $date = $('<div class="input-append date">')
.append($dateField)
.append('<span class="add-on"><i class="icon-calendar">');
var $from = $($.validator.format('<input type="text" id="from{0}" name="from[{0}]" class="input-mini"> <span>(hh:mm)</span>', rowIdx));
var $to = $($.validator.format('<input type="text" id="to{0}" name="to[{0}]" class="input-mini"> <span>(hh:mm)</span>', rowIdx));
if (typeof(stime) !== 'undefined')
{
$id.attr('value', stime.id);
$dateField.attr('value', stime.date);
$from.attr('value', stime.from);
$to.attr('value', stime.to);
}
else
$id.attr('value', -1);
// Attach new input row.
table
.append($('<tr>')
.append($id)
.append($('<td>')
.append($date))
.append($('<td>')
.append($from))
.append($('<td>')
.append($to))
.append($('<td class="vert">')
.append($('<button class="btn btn-mini btnDelTime"><i class="icon-trash">'))));
// Attach rules.
$dateField.rules('add', { required: true });
$from.rules('add', { required: true });
$to.rules('add', { required: true });
// Create pickers.
$date.datepicker({
language: 'de',
autoclose: true,
todayBtn: true,
todayHighlight: true,
}).on('changeDate', function(e) {
editSeminarFormValidator.element($dateField);
$date.datepicker('hide');
});
rowIdx++;
}
Works like a charm!
There seems to be nothing majorly wrong with the logic you're using for adding rules to the new elements. Although, you'll need to attach the .rules()
method to a jQuery object using a jQuery selector, not the element's HTML.
something like...
$('#myInputID').rules('add', {...});
or...
$('input[name="myname"]').rules('add', {...});
But the heart of your problem is in here...
var $id = $('<input type="hidden" name="stid[]">');
var $dateField = $('<input type="text" name="date[]" class="input-mini">');
var $date = $('<div class="input-append date">')
.append($dateField)
.append('<span class="add-on"><i class="icon-calendar">');
var $from = $('<input type="text" name="from[]" class="input-mini"> <span>(hh:mm)</span>');
var $to = $('<input type="text" name="to[]" class="input-mini"> <span>(hh:mm)</span>');
Notice the name
attribute? It's going to be the same for every new row.
The problem is that the jQuery Validate plugin will not work unless each input
element contains a unique name
. Simply modify your code to ensure that a new name
is created for each new element.
EDIT: Indexed arrays will work fine with this plugin. Just increment the index when you create the new element.
name="from[0]"
, name="from[1]"
, name="from[2]"
, etc.
这篇关于jQuery Validation插件不验证动态创建的表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!