我知道可能有几个人问过同样的问题,但我很难找到一个适合我所拥有的解决方案。我有表格,人们可以根据需要添加尽可能多的行(每行有 4 个输入框),然后在不使用时将其删除。我正在使用 .append() jquery 函数来添加行并让它工作。
我很难弄清楚如何验证新的输入框。有人对我想做的事情有很好的解决方案吗?这是我的代码的链接:
$(document).ready(function () {
var count = 1;
$('p#add_field').click(function () {
count += 1;
$('table#entries').append(
'<tr>' +
'<td><input id="cl_' + count + '" name="cl[]' + '" type="text" /></td>' +
'<td><input id="num_' + count + '" name="num[]' + '" type="text" size="5"/></td>' +
'<td><input id="description_' + count + '" name="description[]' + '" type="text" size="86"/></td>' +
'<td><span class="delete">Remove</span></td></tr>');
$(".delete").click(function () {
$(this).closest('tr').remove();
});
});
// 1. prepare the validation rules and messages.
var rules = {
email: {
required: true,
email: true
},
lname: "required",
fname: "required",
city: "required",
address: "required",
'cl[]': "required",
'num[]': "required",
'description[]': "required",
age: {
required: true,
number: true,
maxlength: 3
},
phone: {
required: true,
phoneUS: true
}
}; // end var rules
// 2. Initiate the validator
var validator
= new jQueryValidatorWrapper("FormToValidate",
rules);
// 3. Set the click event to do the validation
$("#btnValidate").click(function () {
if (!validator.validate())
return;
alert("Validation Success!");
});
});