【问题标题】:Laravel: Getting the selected value of a dropdown listLaravel:获取下拉列表的选定值
【发布时间】:2015-05-22 06:06:32
【问题描述】:

我想获取下拉列表的选定值。

下拉列表包含我数据库中的一个表

这是我的下拉列表

 <div class="form-group">
{{ Form::label('student_name', 'Assign a Student to this section:') }}
        {{ Form::select('firstname', $users, null) }}
</div>


<div class="form-group">
{{ Form::label('teacher_name', 'Assign a Teacher to this section:') }}
        {{ Form::select('firstname', $users1, null) }}
</div>

现在,我要获取选中的值

这是我插入记录的位置

        foreach(User::where('isTeacher', '0')->where('isAdmin', '0')->get() as $student) 
        foreach(User::where('isTeacher', '1')->where('isAdmin', '0')->get() as $teacher)

        $section = new Section1();
        $section->name = Input::get('name');
        $section->student_id = $student->id;
       $section->student_firstname = $student->firstname;
       $section->teacher_id = $teacher->id;
       $section->teacher_firstname = $teacher->firstname;

你可能在想我为什么使用 foreach。 到目前为止,我使用我过去的代码作为创建用户的参考,效果很好。

现在,我又用它来录制一段了。

如果您能向我推荐除 foreach 之外的其他代码,我将不胜感激 :)

请帮助大家T_T谢谢。

【问题讨论】:

    标签: laravel laravel-4 eloquent declaration laravel-5


    【解决方案1】:

    您的意思是要将选定的列表项设为“已选择”吗?

    喜欢:

    <select class="js-data-example-ajax">
      <option value="3620194" selected="selected">select2/select2</option>
    </select>
    

    【讨论】:

      【解决方案2】:

      您需要重命名您的选择。他们目前都是firstname - 但他们应该是student_nameteacher_name

      <div class="form-group">
      {{ Form::label('student_name', 'Assign a Student to this section:') }}
              {{ Form::select('student_name', $users, null) }}
      </div>
      
      
      <div class="form-group">
      {{ Form::label('teacher_name', 'Assign a Teacher to this section:') }}
              {{ Form::select('teacher_name', $users1, null) }}
      </div>
      

      然后在您的代码中 - 您可以使用此获取所选名称

      $student_name = Input::get('student_name');
      $teacher_name = Input::get('teacher_name');
      

      【讨论】:

      • @Virgil Cruz - 这对您的问题有帮助吗?您需要更多信息吗?
      猜你喜欢
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      相关资源
      最近更新 更多