我目前正面临两个问题:
if else
,但我似乎不断收到此错误:解析错误:语法错误,意外'<'
有人可以告诉我正确的方法吗?
我试过跟随其他人做的例子,但没有发生任何事情。
我尝试过这里,但仍未检查:
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
答案 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>