我正在开发一个向导,可以从我的后端动态获取员工。生成员工表(带有单选输入字段),然后设置为我的 HTML 代码:
$.ajax({
method: "get",
url: '/getEmployees/',
dataType: 'json',
data: {
ids: JSON.stringify(services)
},
async: false,
success: function(data) {
$.each(data.workers, function(i, v) {
html += "<tr>";
html += "<td class=\"text-center\">";
html += "<label><input type=\"radio\" value=\"" + v.Worker.id + "\" name=\"employeeInput\" id=\"employeeInput\" /></label>";
html += "</td>";
html += "</tr>";
});
$('#employee_items').empty().html(html);
// Add new field
$('#employeeInput').formValidation('addField');
}
});
我正在使用 jQuery 插件“formvalidation.io”验证我的表单输入:
$('#employeeForm').formValidation({
framework: 'bootstrap',
fields: {
employeeInput: {
validators: {
notEmpty: {
message: 'Please choose an employee'
}
}
}
}
});
经过几个小时的尝试,我发现动态生成的字段必须手动添加到表单验证中:
http://formvalidation.io/examples/adding-dynamic-field/
我尝试过,但到目前为止还没有运气。当我动态添加此字段时,如何使用字段 employeeInput
的表单验证?
最佳答案
试试这个:
$('#employeeForm').formValidation('addField', $("#employeeInput"));
关于javascript - 动态添加字段的 jQuery 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682231/