我知道有幾個人可能會問這個問題,但我很難找到一個解決方案來處理我的問題。我有這樣的表單,人們可以根據需要添加儘可能多的行(每行有4個輸入框),然後在不使用時刪除它們。我使用.append()jquery函數添加行並使其工作。使用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!");
});
});
這非常有意義!感謝您的幫助和時間!對此,我真的非常感激! – user2191829 2013-03-26 14:23:43
@ user2191829,不客氣。另外,請不要忘記點擊綠色的選中標記「接受」我的答案。謝謝。 – Sparky 2013-03-26 16:33:07