复选框已选中laravel

时间:2017-12-21 01:24:16

标签: php laravel checkbox

我目前正面临两个问题:

  1. 如果我的驱动程序许可证是肯定的,我想让我的复选框显示,所以我决定在这种情况下尝试使用if else,但我似乎不断收到此错误:
  2.   

    解析错误:语法错误,意外'<'

    有人可以告诉我正确的方法吗?

    1. 我想根据我的数据库值检查我的复选框,例如,如果上次选择了A类和B类并将其保存在数据库中,我希望在我尝试更新其数据时检查它们,以便我知道用户输入了什么
    2. 我试过跟随其他人做的例子,但没有发生任何事情。

      我尝试过这里,但仍未检查:

      https://laracasts.com/discuss/channels/general-discussion/getting-a-checkbox-checked

      以下是代码:

      <div class="editfield">
                    <div class="radio">
                      <span><b>Do you have a Driver`s license?</b></span>
                      <div id="Driver_licenseID">
                         <label><input type="radio" name="Driver_license" id="yesid" value="Yes" onclick="document.getElementById('Driver_license').style.display='block'" {{ $object->Driver_license == 'Yes' ? 'checked' : '' }} >Yes</label>
                        <label><input type="radio" name="Driver_license" id="noid" value="No" onclick="document.getElementById('Driver_license').style.display='none'" {{ $object->Driver_license == 'No' ? 'checked' : '' }}>No</label></div>
                      </div>
                  </div>
                  @if ({{ $object->Driver_license == 'Yes' ? 'checked' : '' }})
                  <div class="editfield" id="Driver_license" required>
                    <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class1" value="Class 1">Class 1 
                    <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2" value="Class 2">Class 2 <br>
                    <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class3" value="Class 3">Class 3 
                  </div>
                  @endif
                  @if({{ $object->Driver_license == 'No' ? 'checked' : '' }})
                  <div class="editfield" id="Driver_license" style="display:none">
                    <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class1" value="Class 1">Class 1 
                    <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2B" 
                    <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2" value="Class 2">Class 2 <br>
                    <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class3" value="Class 3">Class 3 
                  </div>
                  @endif
      

1 个答案:

答案 0 :(得分:1)

首先:

解析错误是由于blade的语法无效。

https://laravel.com/docs/5.5/blade#if-statements

@if ($object->Driver_license == 'Yes')

而不是:

@if({{ $object->Driver_license == 'Yes'}})

第二

要在HTML中选中复选框,只需添加checked属性即可。要有条件地检查它,请使用条件语句来提供checked属性。

尝试以下方法:

<div class="editfield">
  <div class="radio">
    <span><b>Do you have a Driver`s license?</b></span>
    <div id="Driver_licenseID">
        <label><input type="radio" name="Driver_license" id="yesid" value="Yes" onclick="document.getElementById('Driver_license').style.display='block'" {{ $object->Driver_license == 'Yes' ? 'checked' : '' }} >Yes</label>
      <label><input type="radio" name="Driver_license" id="noid" value="No" onclick="document.getElementById('Driver_license').style.display='none'" {{ $object->Driver_license == 'No' ? 'checked' : '' }}>No</label></div>
    </div>
</div>
<div class="editfield" id="Driver_license" required>
  <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class1" value="Class 1">Class 1
  @if ($object->Driver_license == 'Yes')
    <input
      type="checkbox"
      name="Driver_license_class[]"
      id="Driver_license_class2B"
      @if($object->Driver_license_class)
        checked
      @endif> 
  @endif
  <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class2" value="Class 2">Class 2 <br>
  <input type="checkbox" name="Driver_license_class[]" id="Driver_license_class3" value="Class 3">Class 3 
</div>