如果您檢查我的FIDDLE HERE,則會有一個表單被正確驗證。你可以點擊提交按鈕來檢查它是如何工作的。在添加額外的文本字段之前驗證表單
還有一個名爲Add Person的按鈕,它創建一組文本字段。
我的問題是,添加人員按鈕只能在表單完全填充時才創建文本字段。如果表單沒有填寫並且用戶點擊添加人員按鈕,則應該顯示警報。
這裏是我的代碼
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }
});
$().ready(function() {
// validate the comment form when it is submitted
$("#commentForm").validate();
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
topic: {
required: "#newsletter:checked",
minlength: 2
},
agree: "required",
'country': {
required: true
}
},
messages: {
firstname: "Please enter your firstname",
lastname: "Please enter your lastname",
username: {
required: "Please enter a username",
minlength: "Your username must consist of at least 2 characters"
},
email: "Please enter a valid email address",
agree: "Please accept our policy",
country: "Please select an option"
}
});
clicks = 0;
$('a').on('click', function() {
$('.attnd2').show();
$('div.loop').show();
if ($('div.loop').length < 5) {
clicks++;
if(clicks>1){
newLoopDiv = $($('div.loop')[0]).clone().appendTo(".container");
$('input', newLoopDiv).each(function (index, element) {
$(element).attr('name', $(element).attr('name') + clicks);
});
}
else{
newLoopDiv = $($('div.loop')[0]).appendTo(".container");
$('input', newLoopDiv).each(function (index, element) {
$(element).attr('name', $(element).attr('name') + clicks);
});
}
}
});
});
但是,這仍然增加額外的領域,。我需要的是限制添加字段,直到整個表單被填充爲止 – Sowmya
@Sowmya現在修復了它,在條件 –
中錯過了'!'我需要在按鈕下面顯示msg。喜歡請在這之前填寫上述字段 – Sowmya