未捕获的DOMException:无法在'Element'上执行'setAttribute':'1'在Laravel刀片模板中不是有效的属性名称

时间:2019-08-14 07:14:19

标签: php laravel laravel-blade

我的代码有问题,它可以正常工作,但是我从chrome遇到的控制台错误使我感到困惑。

我的刀片服务器模板代码是:

<select name="business_unit_id" class="form-control">
 @foreach (App\Models\BusinessUnit::all() as $data)
  <option value="{{ $data->id }}"
   {{ old('business_unit_id', $cost_centers->business_unit_id ?? null)
    != $data->id ?: 'selected' }}>
    {{ $data->code.' - '.$data->descr }}
  </option>
 @endforeach
</select>

控制台错误为:

app.js:41637 Uncaught DOMException: Failed to execute 'setAttribute' on 'Element': '1' is not a valid attribute name.
    at baseSetAttr (http://ksakuws1137/js/app.js:43932:8)
    at setAttr (http://ksakuws1137/js/app.js:43907:5)
    at Array.updateAttrs (http://ksakuws1137/js/app.js:43862:7)
    at invokeCreateHooks (http://ksakuws1137/js/app.js:43218:22)
    at createElm (http://ksakuws1137/js/app.js:43105:11)
    at createChildren (http://ksakuws1137/js/app.js:43202:9)
    at createElm (http://ksakuws1137/js/app.js:43103:9)
    at createChildren (http://ksakuws1137/js/app.js:43202:9)
    at createElm (http://ksakuws1137/js/app.js:43103:9)
    at createChildren (http://ksakuws1137/js/app.js:43202:9)

罪魁祸首在下面,但这是程序正常运行所必需的。

{{ old('business_unit_id', $cost_centers->business_unit_id ?? null)
    != $data->id ?: 'selected' }}>

有人遇到相同的问题并有解决方案吗?谢谢。

2 个答案:

答案 0 :(得分:0)

我通过从以下位置更改刀片模板中的代码找到了解决方案:

{{ old('business_unit_id', $cost_centers->business_unit_id ?? null)
    != $data->id ?: 'selected' }}>

收件人:

{{ old('business_unit_id', $cost_centers->business_unit_id ?? null)
                                != $data->id ? '' : 'selected' }}

罪魁祸首是“?:”,也许不符合HTML5?

答案 1 :(得分:0)

我也面临这个问题。希望这会帮助其他人寻找答案。对我而言,这是内联刀片@if语句。我不确定,但是认为Vue试图将其解释为某种js方法。

我有这个

<input type="checkbox" name="as_template" @if($page->as_template) ? checked @endif>

必须将其更改为

@if($page->as_template)
    <input type="checkbox" name="as_template" checked>
@else
    <input type="checkbox" name="as_template">
@endif