如果在Laravel foreach中提交后有清单,如何显示清单?

时间:2018-06-08 15:26:44

标签: html laravel-5 laravel-blade

我尝试在foreach blade laravel中提交表单,但为什么在提交后没有检查?



<form method="get" class="form-inline float-left">
   <div class="form-group mb-2">
      <input type="text" id="keyword" name="keyword" class="form-control" placeholder="keyword here" value="{{ $keyword }}">
   </div>
   <ul class="nav nav-pills" id="nav">
      @foreach($menu as $d)
      <li role="presentation">
         <a class="text-capitalize" >
         <input type="checkbox" name="cx[]" value="{{ $d->custom_search_id }}" {!! (is_array(old('cx[]')) and in_array($d->custom_search_id, old('cx[]'))) ? ' checked' : '' !!} > 
         {!! $d->name !!}
         </a>
      </li>
      @endforeach
   </ul>
   <div class="form-group mb-2">
      <input type="submit" class="btn btn-primary" value="GO!">
   </div>
</form>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

如果要检查旧输入(具有多个值),则在检查旧值时不应使用带方括号的输入名称(不是old('cx[]')而是old('cx'),例如:< / p>

   <ul class="nav nav-pills" id="nav">
      @foreach($menu as $d)
      <li role="presentation">
         <a class="text-capitalize">
         <input type="checkbox" name="cx[]" value="{{ $d->custom_search_id }}" {!! (is_array(old('cx')) and in_array($d->custom_search_id, old('cx'))) ? ' checked' : '' !!} > 
         {!! $d->name !!}
         </a>
      </li>
      @endforeach
  </ul>

答案 1 :(得分:0)

我找到了答案,创建了一些像fisrt这样的函数:

&#13;
&#13;
<?php
   function checkCX($id){
   foreach(\Request::input("cx") as $input){
   if($input == $id){
   return true;
   }
   }
   return false;
   }
   ?>
   
<form method="get" class="form-inline float-left">
   <div class="form-group mb-2">
      <input type="text" id="keyword" name="keyword" class="form-control" placeholder="keyword here" value="{{ $keyword }}">
   </div>
   <ul class="nav nav-pills" id="nav">
      @foreach($menu as $d)
      <li role="presentation">
         <a class="text-capitalize" >
         <input type="checkbox" name="cx[]" value="{{ $d->custom_search_id }}" @if(!empty($d->custom_search_id)) {{ checkCX($d->custom_search_id) ? 'checked' : '' }} @else @endif > 
         {!! $d->name !!}
         </a>
      </li>
      @endforeach
   </ul>
   <div class="form-group mb-2">
      <input type="submit" class="btn btn-primary" value="GO!">
   </div>
</form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

<强> #CMIW

<form method="get" class="form-inline float-left">
  <div class="form-group mb-2">
     <input type="text" id="keyword" name="keyword" class="form-control" placeholder="keyword here" value="{{ $keyword }}">
  </div>
  <ul class="nav nav-pills" id="nav">
     @foreach($menu as $d)
     <li role="presentation">
        <a class="text-capitalize" >
           <input type="checkbox" name="cx[]" value="{{ $d->custom_search_id }}" {!! (is_array(old('cx')) and in_array($d->custom_search_id, old('cx'))) ? 'checked' : null !!}>
           {!! $d->name !!}
        </a>
     </li>
     @endforeach
  </ul>
  <div class="form-group mb-2">
     <input type="submit" class="btn btn-primary" value="GO!">
  </div>
</form>