简体   繁体   中英

jQuery form validation for dynamically added field

I am working on a wizard that dynamically gets employees from my backend. The employee table is generated (with a radio-input-field) and then set to my HTML code:

$.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');
    }
});

I am validating my form-inputs with the jQuery plugin "formvalidation.io":

$('#employeeForm').formValidation({
    framework: 'bootstrap',
    fields: {
        employeeInput: {
            validators: {
                notEmpty: {
                    message: 'Please choose an employee'
                }
            }
        }
    }
});

After trying for hours I found out that dynamically generated fields have to be added to the form validation manually:

http://formvalidation.io/examples/adding-dynamic-field/

I tried it but had no luck so far. How can I use the form-validation for the field employeeInput when I add this field dynamically?

尝试这个:

$('#employeeForm').formValidation('addField', $("#employeeInput"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM