【问题标题】:Populate dropdown based on another dropdown selected value in laravel - edit form根据 laravel 中的另一个下拉选择值填充下拉列表 - 编辑表单
【发布时间】:2015-11-03 14:31:05
【问题描述】:

我找到了如何在另一个网站上完成此操作,并且我得到了它的工作。我的问题是我需要在我的编辑表单中使用它(最好与我的创建表单使用相同的表单,因为我的创建和编辑视图使用相同的表单)。当我转到我的编辑页面时,“部分”下拉值被选中。但是“子部分”不是,因为我当然没有点击它。可能这对我来说太容易看到了,但我对 javascript 不太擅长。我基本上需要第二个下拉列表(小节之一)在我的编辑视图中根据选择的“部分”(第一个下拉列表)显示正确的选项。

截面模型:

public function subsections()
{
    return $this->hasMany('App\Subsection');
}

分段模型:

public function section()
{
    return $this->belongsTo('App\Section');
}

查看:

{!! Form::select('section_id', [null=>'-- Select Section --'] + $sections, null, array('id' => 'section')) !!}

<select id="subsection" name="subsection_id" class="select">
    <option>-- First Select Section --</option>
</select>

From my controller: $sections = Section::lists('section', 'id');

脚本:

<script>
$(document).ready(function($){
    $('#section').change(function(){
        $.get("{{ url('api/dropdown')}}", 
        { option: $(this).val() }, 
        function(data) {
            $('#subsection').empty(); 
            $.each(data, function(key, element) {
                $('#subsection').append("<option value='" + key +"'>" + element + "</option>");
            });
        });
    });
});
</script>

路线:

Route::get('api/dropdown', function(){
    $id = Input::get('option');
    $subsections = Section::find($id)->subsections;
    return $subsections->lists('subsection', 'id');
});

【问题讨论】:

    标签: php jquery ajax laravel dropdown


    【解决方案1】:

    如果你只想在jquery中选择第一个选项,你可以添加:

    $('#subsection option:eq(1)').prop('selected', true);
    

    或者如果你想选择一个特定的值,你可以这样做

    $('#subsection option[value="value_to_be_selected"]').prop('selected', true);
    

    在将所有值附加到小节后使用它

    【讨论】:

    • 听起来不错,但主要问题是子部分下拉菜单不会自动填充。当我在部分下拉列表中选择某些内容时会这样做,但在我的编辑页面上,我希望显示、填充部分和子部分下拉列表并选择正确的值。所以也许这是一个比我预期的更困难的问题? :)
    • 好吧,我假设您使用不同的视图进行编辑和创建并包含相同的表单,所以您可以做的是,在编辑页面中添加一个额外的脚本来设置值您将从控制器/路由功能编辑方法传递的表单。
    • 我顿悟了。在我的表单上,我只显示带有查询 where section = ... 的子部分下拉列表,依此类推。一旦我更改了部分,子部分下拉菜单也将更改。问题解决了。我会发布我自己的答案。
    • @BharatGeleda 请您对我的回答有何看法?
    【解决方案2】:

    在我的控制器中我添加了:

        $subsections = Subsection::where('section_id', '=', $transaction->section_id)->orderBy('subsection', 'asc')->lists('subsection', 'id');
    

    在我看来,我是这样做的:

        @if(isset($transaction))
        {!! Form::select('subsection_id', [null=>'-- Select Subsection --'] + $subsections, null, array('class' => 'select', 'id' => 'subsection')) !!}
        @else
            <select id="subsection" name="subsection_id" class="select">
                <option>-- First Select Section --</option>
            </select>
        @endif
    

    问题解决了:)

    【讨论】:

      【解决方案3】:

      所以,我们将首先将值发送到依赖下拉列表
      "&lt;option value=""&gt;Select Something&lt;/option&gt;" 当返回错误时,创建完整的下拉菜单并返回,这将减少大量的 javascript 调用。
      控制器:

      if($error){
      //create get array of second drop-down, suppose $second_drop
      // create complete second drop-down like
      $selectbox="";
      foreach($second_drop as $option){
                  $selectbox.="<option value='". $option->id."'";
                  if($request->option2==$ $option->id){
                      $selectbox.=" selected ";
                  }
      
                  $selectbox.=">".$option->value."</option>";
              }
      }
      return view('routename', ['selectbox2' =>   $selectbox]);
      

      查看:

      <select id="subsection" name="subsection_id" class="select">
          {!! $selectbox !!}
      </select>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-11
        相关资源
        最近更新 更多