2016-07-17 20 views
0

在我的表單中,我有幾個字段,可以由用戶動態添加。Laravel 5重定向頁面,在驗證失敗後動態添加字段

我通過JQuery(functions.js)添加字段。目前,我的工作沒有數組,而我正在使用隱藏字段,其中存儲了字段數。

$("#weiterer_artikel").on("click", function(){ 
    //read the value of the hidden field 
    $anzahl = $("#bestellanzahl").val(); 

    //$anzahl is a String 
    $anzahl = parseInt($anzahl); 
    $neueAnzahl = $anzahl+1; 

    //HTML code for a new article 
    $nummer = "<div class='formgroup'><label for='nummer-"+$neueAnzahl+"' class='col-sm-2 control-label' id='nr-"+$neueAnzahl+"'>Nr.</label>"+ 
    "<div class='col-sm-10'><input type='text' name='nummer-"+$neueAnzahl+"' value='"+$neueAnzahl+"' class='form-control' readonly></div></div>"; 
    $artikel = "<div class='formgroup'> <label for='artikel-"+$neueAnzahl+"' class='col-sm-2 control-label' id='artikel-"+$neueAnzahl+"'>Artikel</label>"+ 
    "<div class='col-sm-10'><input type='text' class='form-control' placeholder='Artikelname' name='artikel-"+$neueAnzahl+"' required></div></div>"; 
    $artnr = "<div class='formgroup'> <label for='art-nr-"+$neueAnzahl+"' class='col-sm-2 control-label' id='art-nr-"+$neueAnzahl+"'>Art.Nr.</label>"+ 
    "<div class='col-sm-10'><input type='text' class='form-control' placeholder='Artikelnummer' name='art-nr-"+$neueAnzahl+"'></div></div>"; 
    $stk = "<div class='formgroup'> <label for='anzahl-"+$neueAnzahl+"' class='col-sm-2 control-label' id='anzahl-"+$neueAnzahl+"' required>Stk.</label>"+ 
    "<div class='col-sm-10'><select class='form-control' id='anzahl-"+$neueAnzahl+"' name='anzahl-"+$neueAnzahl+"'>"+ 
    "<option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select></div></div>"; 

    //add field 
    $("#bestellung-"+$anzahl).after("<div id='bestellung-"+$neueAnzahl+"'>"+$nummer+$artikel+$artnr+$stk+"</div>"); 

    //write new field number in hidden field 
    $("#bestellanzahl").val($neueAnzahl); 


}); 

我的問題是,如果我發送的形式(通過POST)和服務器端驗證(通過表單請求)失敗,所有手動添加字段從我的視圖中刪除。

有什麼方法可以解決我的問題嗎?

回答

0

您應該使用ajax提交表單,或者您可以將您動態添加的html存儲到cookie。

+0

謝謝!我會嘗試與AJAX :) – Scoy

+0

@Scoy,如果它的工作,請採取我的answer.tks –

相關問題