【问题标题】:how to show checkbox checked in laravel如何显示在laravel中选中的复选框
【发布时间】:2021-08-03 15:21:21
【问题描述】:

您好,我正在处理多个复选框,并且复选框的值以这种格式存储在我的数据库 2、3、4
我的刀片文件的代码:-

<table class="table table-striped">
        @foreach($var as $vars)

                     <tbody>
                      <tr>
                       <td>
                        <input type="checkbox" class="larger" name="process[]"  value=""  {{ ($vars->process == 1 ? 'checked' : '')}}>Cold/Hot Forging
                       </td>
                      </tr>
                      <tr>
                        <td>
                        <input type="checkbox" class="larger" name="process[]" value=""  {{ ($vars->process == 2 ? 'checked' : '')}}>Injection Molding
                       </td>
                       </tr>
                       <tr>
                          <td>
                        <input type="checkbox" class="larger" name="process[]" value=""  {{ ($vars->process == 3 ? 'checked' : '')}}>Precision Machining
                       </td>
                       </tr>
                       <tr>
                          <td>
                        <input type="checkbox" class="larger" name="process[]" value=""  {{ ($vars->process == 4 ? 'checked' : '')}}>Sheet Metal Pressing
                       </td>
                       </tr>
                       <tr>
                          <td>
                        <input type="checkbox" class="larger" name="process[]" value=""  {{ ($vars->process == 5 ? 'checked' : '')}}>Sub-assembly
                       </td>
                       </tr>
                       <tr>
                          <td>
                        <input type="checkbox" class="larger" name="process[]" value=""  {{ ($vars->process == 6 ? 'checked' : '')}}>Surface tretment
                       </td>
                       </tr>
                       <tr>
                          <td>
                        <input type="checkbox" class="larger" name="process[]" value=""  {{ ($vars->process == 7 ? 'checked' : '')}}> Other
                       </td>
                       </tr>
                    </tbody>
                  @endforeach
               </table>

所以这段代码只显示了一个选中的复选框,其值为 2 控制器代码:-

$processvalue = ProcessValue::where('company_basic_info_id','=',$id)->get(); 

【问题讨论】:

  • 您存储的是逗号分隔的值字符串 (2,3,4)?
  • 如果$vars-&gt;process = '1,2,3',这将永远不会是真的$vars-&gt;process == 1

标签: javascript php mysql ajax laravel


【解决方案1】:

为了实现这一点,

首先转换数组中的数据库值,

$var = '2,3,4';   // Your checkbox values in database
$allCheckboxValues = explode(",",$var);  // Convert it in array

然后把它放在你的复选框中,

<table class="table table-striped">
  <tbody>
    <tr>
      <td>
        <input type="checkbox" class="larger" name="process[]"  value="1"  {{ (in_array(1,$allCheckboxValues) ? 'checked' : '')}}>Cold/Hot Forging
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="larger" name="process[]" value="2"  {{ (in_array(2,$allCheckboxValues) ? 'checked' : '')}}>Injection Molding
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="larger" name="process[]" value="3"  {{ (in_array(3,$allCheckboxValues) ? 'checked' : '')}}>Precision Machining
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="larger" name="process[]" value="4"  {{ (in_array(4,$allCheckboxValues) ? 'checked' : '')}}>Sheet Metal Pressing
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="larger" name="process[]" value="5"  {{ (in_array(5,$allCheckboxValues) ? 'checked' : '')}}>Sub-assembly
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="larger" name="process[]" value="6"  {{ (in_array(6,$allCheckboxValues) ? 'checked' : '')}}>Surface tretment
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" class="larger" name="process[]" value="7"  {{ (in_array(7,$allCheckboxValues) ? 'checked' : '')}}> Other
      </td>
    </tr>
  </tbody>
</table>

【讨论】:

    【解决方案2】:

    首先你创建一个包含所有数据的数组

    <?php 
    $all_processvalue = array();
    foreach($processvalue as $pvalue){
         $all_data[] =  $pvalue->id;
    }
    ?>
    

    现在你创建复选框

    @foreach ($vars as $var)
        {{ Form::checkbox('var[]', $var->id, in_array($var->id, $all_processvalue )) }}
        {{ Form::label('var', $var->name) }}<br>
    @endforeach
    

    【讨论】:

    • 等我查一下
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多