【问题标题】:Setting selected option in laravel form以 laravel 形式设置选定的选项
【发布时间】:2016-07-18 03:50:38
【问题描述】:

我需要像这样的 html 给出选定的值:

<select name="myselect" id="myselect">
 <option value="1">Item 1</option>
 <option value="2" selected='selected'>Item 2</option>

如何使用 laravel 表单实现这一点?

【问题讨论】:

    标签: php laravel laravel-5.2


    【解决方案1】:

    所有谈论你的人都使用{!! Form::select() !!} 但是,如果您只需要使用简单的 HTML。这是另一种方法。

    <select name="myselect">
    @foreach ($options as $key => $value)
        <option value="{{ $key }}"
        @if ($key == old('myselect', $model->option))
            selected="selected"
        @endif
        >{{ $value }}</option>
    @endforeach
    </select>
    

    old() 函数在您提交表单但验证失败时很有用。这样,old() 返回之前选择的值。

    【讨论】:

      【解决方案2】:

      你可以这样做。

      <select class="form-control" name="resoureceName">
      
        <option>Select Item</option>
      
        @foreach ($items as $item)
          <option value="{{ $item->id }}" {{ ( $item->id == $existingRecordId) ? 'selected' : '' }}> {{ $item->name }} </option>
        @endforeach    </select>
      

      【讨论】:

      • 优雅的答案。
      • 简单实用
      【解决方案3】:

      您必须通过传递第三个参数来设置默认选项。

      {{ Form::select('myselect', [1, 2], 2, ['id' => 'myselect']) }}
      

      您可以阅读文档here.

      【讨论】:

      • @JuniorMinKhant 很高兴知道它的帮助。请将此标记为答案,以便其他人发现它有帮助。
      • 这适用于 Laravel 4.2 .. 如果你想在 Laravel 5.1+ 上使用 Form::select,你需要拉入 laravelcollective/html
      • 如果它是一个多选并检查多个项目怎么办?我有一个带有复选框的多选,请参阅multiselect-ui
      【解决方案4】:

      使用这个包并检查文档:

      https://laravelcollective.com/docs/5.2/html#drop-down-lists

      形成你的html,你需要使用这个标记

      {!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); !!}
      

      【讨论】:

      【解决方案5】:

      另一种普通的简单方法如果选择框中的选项很少,这很好

      <select name="job_status">
         <option {{old('job_status',$profile->job_status)=="unemployed"? 'selected':''}}  value="unemployed">Unemployed</option>
         <option {{old('job_status',$profile->job_status)=="employed"? 'selected':''}} value="employed">Employed</option>
      </select>
      

      【讨论】:

        【解决方案6】:

        试试这个

        <select class="form-control" name="country_code" value="{{ old('country_code') }}">
           @foreach (\App\SystemCountry::orderBy('country')->get() as $country)
               <option value="{{ $country->country_code }}"
                   @if ($country->country_code == "LKA")
                     {{'selected="selected"'}}
                   @endif
               >
                  {{ $country->country }}
               </option>
           @endforeach
        </select>
        

        【讨论】:

        • 看来你的帖子主要是代码,请补充一些细节
        【解决方案7】:
                    @foreach ($categories as $category)
                     <option value="{{$category->id}}" 
                       @foreach ($posts->postRelateToCategory as $Postcategory)
                         @if ($Postcategory->id == $category->id)
                         {{'selected="selected"'}}
                         @endif 
                       @endforeach >
                      {{ $category->category_name }} </option>               
                    @endforeach    
        

        【讨论】:

        • 优雅的回答@Mahedi Hasan Durjoy
        【解决方案8】:
          <?php
              $items = DB::table('course')->get()->pluck('name','id');
              $selectID = 3;
          ?>
        
          <div class="form-group">
           {{ Form::label('course_title', 'Course Title') }}
           {!! Form::select('myselect', $items, $select, ['class' => 'form-control']) !!}
          </div>
        

        这显示了类似类型的以下选项:

        <select name="myselect" id="myselect">
         <option value="1">Computer Introduction</option>
         <option value="2">Machine Learning</option>
         <option value="3" selected='selected'>Python Programming</option>
         <option value="4">Networking Fundamentals</option>
         .
         .
         .
         .  
        </select>
        

        【讨论】:

          【解决方案9】:

          如果您的模型之间存在 Eloquent Relationship,您可以这样做:

          @foreach ($ships as  $ship)
          <select name="data[]" class="form-control" multiple>
              @foreach ($goods_containers as  $container)
                  <option value="{{ $container->id }}"
                          @if ($ship->containers->contains('container_id',$container->id ) ))
                          selected="selected"
                          @endif
                  >{{ $container->number}}</option>
              @endforeach
          </select>
          @endforeach
          

          【讨论】:

            【解决方案10】:

            在 laravel 形式中设置选择的选项非常简单:

            {{ Form::select('number', [0, 1, 2], 2) }}
            

            输出将是:

            <select name="number">
              <option value="0">0</option>
              <option value="1">1</option>
              <option value="2" selected="selected">2</option>
            </select>
            

            【讨论】:

            • 您在大约 2 小时后回答了与 @chonchol 相同的答案?我会指出山雀,但我首先指出要回答。还是有人编辑了他们的?
            【解决方案11】:

            在这里回应一些其他的答案,我刚刚在 5.6 中使用的代码是这样的

            {{ Form::select('status', ['Draft' => 'Draft', 'Sent' => 'Sent', 'Paid' => 'Paid'], $model->status, ['id' => 'status']) }}
            

            为了能够使用 LaravelCollective 的表单助手,我查看了https://laravelcollective.com/docs/master/html#drop-down-lists

            我还必须作曲家需要依赖项,以便我可以在我的项目中使用它

            composer require "laravelcollective/html":"^5"
            

            最后我更改了我的config/app.php 并在$aliases 数组中添加了以下内容

                'Form' => Collective\Html\FormFacade::class,
            

            如果上述任何一项停止工作,应咨询https://laravelcollective.com/docs/master/html

            【讨论】:

              【解决方案12】:

              只需粘贴此代码,您将获得所需的输出。

               {{ Form::select ('myselect', ['1' => 'Item 1', '2' => 'Item 2'], 2 , ['id' =>'myselect']) }}` `
              

              【讨论】:

              • 只需粘贴此代码,您将获得所需的输出。
              • 感谢 Ashish 在适当的缩进上制作代码我是 stackoverflow 的新手。我认为这段代码可以解决您的问题的任何方式
              【解决方案13】:

              您也可以在有限的选项中尝试这个:

                        <select class="form-control required" id="assignedRole">
                          <option id = "employeeRole" selected ="@if($employee->employee_role=='Employee'){'selected'}else{''} @endif">Employee</option>
                          <option id = "adminRole" selected ="@if($employee->employee_role=='Admin'){'selected'}else{''} @endif">Admin</option>
                          <option id = "employerRole" selected ="@if($employee->employee_role=='Employer'){'selected'}else{''} @endif">Employer</option>
                        </select>
              

              【讨论】:

                【解决方案14】:

                如果有人还在这里,这是我的简单版本。

                <select name="status" class="js-example-basic-single w-100">
                              <option value="" @if($brand->status == '') ? selected : null @endif disabled>Choose what to be seen on brand section</option>
                              <option value="TA" @if($brand->status == 'TA') ? selected : null @endif>Title Active</option>
                              <option value="ITA" @if($brand->status == 'ITA') ? selected : null @endif>Image Title Active</option>
                             
                            </select>
                

                【讨论】:

                  【解决方案15】:

                  这似乎是最短的代码

                  <select class="form-control" name="country_code">
                      @foreach (\App\SystemCountry::orderBy('country')->get() as $country)
                         <option value="{{ $country->country_code }}"
                             @if ($country->country_code == "LKA") selected="selected" @endif >{{ $country->country }}
                         </option>
                      @endforeach
                  </select>
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-10-04
                    • 1970-01-01
                    • 2015-09-19
                    • 2020-05-06
                    • 2020-04-18
                    • 2015-03-19
                    • 1970-01-01
                    相关资源
                    最近更新 更多