1 回答

TA貢獻1757條經驗 獲得超7個贊
我不知道您的系統如何替換<field-yield>標簽,但我想會有典型的表單字段。所以你需要編寫一些通用的驗證函數,它會一步一步驗證所有字段,如果驗證通過,則允許用戶繼續下一步。
在步驟中驗證所有輸入字段的函數:
function validateStep(step) {
var stepWrapper = $('fieldset#step'+step);
var emptyFields = 0;
$('input[type="text"], select, textarea', stepWrapper).each(function(i, el) {
if ($(el).val() == '') {
emptyFields++;
return false;
}
});
//check radio buttons
var radioGroups = {};
//group radio buttons by name
$(':radio', stepWrapper).each(function() {
radioGroups[this.name] = true;
});
//count "checked" radio buttons in groups
for (var radioName in radioGroups) {
var checked = !!$(':radio[name="'+radioName+'"]:checked', stepWrapper).length;
if (!checked) {
emptyFields++;
}
}
return emptyFields;
}
現在您有了步驟驗證,您唯一需要做的就是在步驟更改之前調用此函數:
$("#next").click(function () {
//validate before move
if (validateStep(currentPage)) {
alert("Please fill all fields before move to next step!");
return false;
}
if (currentPage < maxPage) {
var nextPage = currentPage + 1;
} else {
return; //if already at max page
}
//... rest of your code
}
希望這對你有幫助。
更新: 這是您的頁面表單的一個工作示例:https : //codepen.io/zur4ik/pen/LYYqjgM 我在每個循環中都有一個錯誤。
添加回答
舉報