【问题标题】:Get selected value in form, seletbox获取表单、选择框中的选定值
【发布时间】:2015-01-27 04:40:37
【问题描述】:

我有一个问题,我无法解决,请帮助我。所以我有我的表格:

{{ Form::open(array('url'=>'/administration/student/addMarks','method' => 'post')) }}
         @foreach($aObjectsInGroupe as $object)
             {{ Form::hidden('id_object[]',$object->id)   }}
             {{ Form::label($object->name) }}
             {{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
             <br />
         @endforeach
            {{ Form::hidden('id',$aOneStudent['id']) }}
            {{ Form::submit('Add mark',array('class'=>'btn btn-primary')) }}
{{ Form::close() }}

在我的 StudentController 中,我有一个从 student_id 和 object_id 获取标记的方法:

public function getMarkByStudentAndObject($nIdStudent, $nIdObject){
    $aMark = \Mark::where('student_id', '=', $nIdStudent)
                ->and('object_id', $nIdObject)
                ->get()
                ->toArray();
}

$aMarsks 这是一张桌子:

$aMarks = array(
        '0'=>'0',
        '1'=>'1',
        '2'=>'2',
        '3'=>'3',
        '4'=>'4',
        '5'=>'5',
        '6'=>'6',
        '7'=>'7',
        '8'=>'8',
        '9'=>'9',
        '10'=>'10',
    );

可以调用getMarkByStudentAndObject方法:

{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}

获取选定的值? 请帮帮我。提前谢谢。

【问题讨论】:

    标签: php laravel laravel-4 laravel-3


    【解决方案1】:

    也许你需要这样做:

    <select name="note[]" class="form-control">
        @foreach($aMarks as $key => $value)
            <option value="{{ $key }}">{{ $value }}</option>
        @endforeach
    </select>
    

    您还应该检查您的 Eloquent 查询,而不是 -> 并使用另一个 ->where

    【讨论】:

      【解决方案2】:

      试试查询生成器的lists() 功能

      \Mark::where('student_id', '=', $nIdStudent)
                  ->and('object_id', $nIdObject)
                  ->lists('column_1', 'column_2');
      

      然后将 column_1 的值作为数组的键,将 column_2 的值作为值。

      http://laravel.com/docs/4.2/queries#selects

      【讨论】:

        【解决方案3】:

        您应该可以直接从Form::select 调用它。打电话给

        Mark::getMarkByStudentAndObject($aOneStudent['id'], $object->id)->note
        

        会给您Mark 对象,然后您需要获取将值存储在Mark 对象中的注释列。导致这样的调用:

        {{
            Form::select(
                'note[]',
                $aMarks, 
                Mark::getMarkByStudentAndObject(
                    $aOneStudent['id'], 
                    $object->id
                )->note, array('class'=>'form-control')
            )
        }}
        

        目前我无法验证这是否有效,因为我无法对其进行测试,但它应该可以工作,因为 Blade 只是 PHP 调用的包装器。

        【讨论】:

          猜你喜欢
          • 2017-03-23
          • 2013-09-06
          • 2018-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-07
          • 2018-09-19
          • 1970-01-01
          相关资源
          最近更新 更多