Validate dynamically added control

后端 未结 2 1161
挽巷
挽巷 2021-01-12 02:15

How can I add the dynamically added control to validation?

@*@Html.EditorFor(model => model.Middlename)*@
2条回答
  •  醉酒成梦
    2021-01-12 02:39

    UPDATE: This answer is if you use jquery unobtrusive validation, you did not wrote what you use.

    First, add this plugin to jQuery ready: http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/

    Mind that you must add your element inside of form tag. Lets say that you have some wrapper around your tag, I will call it "element".

    After you append your element to DOM, call this:

    $.validator.unobtrusive.parseDynamicContent($(element).find("form").selector);
    var form = $(element).find("form");
    $(form).valid();
        $(form).find("input").each(function () {
            $(this).blur(function () {
                $(this).valid();
            });
        });
        $(form).find("select").each(function () {
            $(this).change(function () {
                $(this).valid();
            });
    

提交回复
热议问题