Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

jquery - Validate dynamically added fields

I'm adding a table row with jQuery on button click:

$("#add_row").click(function () {
        zeile++;
        $("#ArtikelTabelle > tbody").append('<tr id="reihe' + zeile + '">' +
        '<td rowspan="2"><b>' + (zeile + 1) + '</b></td>' +
        '<td><input class="form-control" id="ccTabelle_' + zeile + '__ccArtikelNr" name="ccTabelle[' + zeile + '].ccArtikelNr" type="text"  /></td>' +
        '<td><input class="form-control" id="ccTabelle_' + zeile + '__ccWarentarifNr" name="ccTabelle[' + zeile + '].ccWarentarifNr" type="text" /></td>' +
        '<td><input class="form-control" data-val="true" data-val-number="Das Feld &quot;Anzahl&quot; muss eine Zahl sein." data-val-required="Das Feld &quot;Anzahl&quot; ist erforderlich." id="ccTabelle_' + zeile + '__ccAnzahl" name="ccTabelle[' + zeile + '].ccAnzahl" type="text" /></td>' +
        '<td><input class="form-control" data-val="true" data-val-number="Das Feld &quot;Einzelpreis&quot; muss eine Zahl sein." data-val-required="Das Feld &quot;Einzelpreis&quot; ist erforderlich." id="ccTabelle_' + zeile + '__ccEinzelpreis" name="ccTabelle[' + zeile + '].ccEinzelpreis" type="text" /></td>' +
        '<td><input class="form-control" data-val="true" data-val-number="Das Feld &quot;Gesamtpreis&quot; muss eine Zahl sein." data-val-required="Das Feld &quot;Gesamtpreis&quot; ist erforderlich." id="ccTabelle_' + zeile + '__ccGesamtpreis" name="ccTabelle[' + zeile + '].ccGesamtpreis" type="text" /></td>' +
        '<td><input class="form-control" data-val="true" data-val-number="Das Feld &quot;Anzahl Pakete&quot; muss eine Zahl sein." data-val-required="Das Feld &quot;Anzahl Pakete&quot; ist erforderlich." id="ccTabelle_' + zeile + '__ccAnzahlPakete" name="ccTabelle[' + zeile + '].ccAnzahlPakete" type="text" /></td>' +
        '<td><input class="checkbox-inline" data-val="true" data-val-required="Das Feld &quot;Fertig verpackt?&quot; ist erforderlich." id="ccTabelle_' + zeile + '__ccFertigVerpackt" name="ccTabelle[' + zeile + '].ccFertigVerpackt" type="checkbox" /><input name="ccTabelle[' + zeile + '].ccFertigVerpackt" type="hidden" /></td>' +
        '<td rowspan="2"><a class="btn btn-default delete_row" data-rowid="' + zeile + '">Artikel l?schen</a></td>' +
    '</tr>' +
    '<tr id="text' + zeile + '">' +
        '<td colspan="3"> <textarea class="form-control input-md" id="ccTabelle_' + zeile + '__ccInhaltsbeschreibung" name="ccTabelle[' + zeile + '].ccInhaltsbeschreibung" placeholder="Inhaltsbeschreibung"></textarea> </td>' +
        '<td colspan="4"> <textarea class="form-control input-md" id="ccTabelle_' + zeile + '__ccBemerkungInhalt" name="ccTabelle[' + zeile + '].ccBemerkungInhalt" placeholder="Bemerkungen"></textarea> </td>' +
    '</tr>');

        aktualisiereZeilennummer();

    });

I also use this content to initially create a row.

But on clicking button submit I want to validate these fields if they are filled or not an highlight those fields.

I tried using $("#form").validate(); but I don't really know how to use it and it didn't work.

Also the validation in my model failed:

    public class InhaltModel
{
    //Inhalt
    [Required]
    [Display(Name = "Artikelnummer")]
    public string ccArtikelNr { get; set; }
    [Required]
    [Display(Name = "Warentarif-Nr")]
    public string ccWarentarifNr { get; set; }
}

If I use Razor to add a row it works but not with jQuery, but i need it dynamic so that I can add as many rows as I want to.

Can anyone tell me how to validate my fields ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to parse the form again after adding dynamic content in order for that content to be validated

$('form').data('validator', null);
$.validator.unobtrusive.parse($('form'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...