2014-10-31 94 views
0

我在laravel应用程序中进行约束。 我有一个窗体,我想用JavaScript显示复选框。 我正在从数据库中获取复选框的值。重新填充表单中的复选框(laravel)

这里是我的代码:

$.ajax({ 
    url: '/home/show', 
    type: 'GET', 
    data: 'id=' + selection, 
    success: function(data) { 
     //Build the checkboxes and the label for the termids 
     var items ='{{Form::label("Linkids","Linkids", array("class" => " col-sm-2 control-label")) }}'; 
     items += '<table class="table table-borderless" style="margin-right:10%;margin-left:90%"><tr style="border: 0;text-align: center;">'; 
     /* The variable j plays an important role in displaying the termids in rows since a borderless table was created for them */ 
     var j = 1; 
     $.each(data, function (i, terms) { 
      if(j < 8){ 
       items +='<label><td style="border: 0; text-align: center;">'+ 
        **'<input style = "margin-left: 26px; margin-right: 0px;" type="checkbox" name="termids[]" value="'+terms.NewTermID+'"><p style="text-align:right">'**+'<b>'+terms.NewTermID+'</p>'+ 
       '</td></label>'; 
       j++; 
      } 
      else{ 
       j = 1; 
       items += '</tr><tr>' ; 
      }       
     }); 
     items += '</tr></table>'; 
     $('#rData').html(items); 
     $('#rData').show(); 
    } 
}); 

后,我想提交表单的重新填充与所提交的数据的复选框。 在我controoler我使用验证:

if ($validator->fails()) 
{ 
    return Redirect::back()->withInput() 
          ->withErrors($validator) 
          ->with('message', 'There were validation errors.'); 
} 

我试图用Input::old(terms)但我只是得到所有的复选框选中!而不是提交的复选框。

任何帮助将非常感激。

在此先感谢。

回答

0

如果我是正确的,您正在使用Blade语法和普通html组合的形式, 这很好,但我认为Laravel的自动完成/重新填充过程适用于Blade语法元素。

您是否试过Blade only语法?

{{ Form::checkbox('name', 'value', true) }} 

第三个参数“真”表示如果一个复选框将被选中或不