【问题标题】:Repopulating checkboxes in form (laravel)重新填充表单中的复选框(laravel)
【发布时间】:2014-10-31 10:02:36
【问题描述】:

我正在使用 laravel 应用程序。 我有一个表单,我想用 javascript 显示复选框。 我正在从数据库中获取复选框的值。

这是我的代码:

$.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();
    } 
});

我希望在提交表单后用提交的数据重新填充复选框。 在我的控制器中,我正在使用验证器:

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

我尝试使用Input::old(terms),但我只是选中了所有复选框!而不是提交的复选框。

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: javascript forms laravel checkbox repopulation


    【解决方案1】:

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

    你试过 Blade only 语法吗?

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

    第三个参数“true”表示是否选中复选框

    【讨论】:

      猜你喜欢
      • 2019-05-13
      • 2019-04-18
      • 2015-01-08
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2014-10-08
      相关资源
      最近更新 更多