Jquery Validation for Dynamically Created Fields

前端 未结 1 1985
生来不讨喜
生来不讨喜 2020-11-29 13:02

I have basic javascript code to generate input text areas as below

$(\"#btnAdd\").click(function (e) {
        var itemIndex = $(\"#container input.iHidden\         


        
1条回答
  •  萌比男神i
    2020-11-29 13:40

    You could use the .rules('add') method immediately after the new input element is created...

    $("#btnAdd").click(function (e) {
        var itemIndex = $("#container input.iHidden").length;
        e.preventDefault();
        var newItem = $("" + itemIndex + "      

    "); $("#container").append(newItem); // add the rules to your new item $('Interests_' + itemIndex + '__Id').rules('add', { // declare your rules here required: true }); });

    Alternatively, for a simple rule like required, you could just add the required="required" attribute to the new element when you create it...

    $("#btnAdd").click(function (e) {
        var itemIndex = $("#container input.iHidden").length;
        e.preventDefault();
        var newItem = $("" + itemIndex + "      

    "); $("#container").append(newItem); });

    0 讨论(0)
提交回复
热议问题