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

I have a problem in my code, it is functional and but it bugs me with the console error I am getting from chrome.

My blade template code is:

<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>

The console error are:

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)

The culprit is below, but that is needed to in order for the program to behave correctly.

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

Anyone encounter the same issue and have a resolution? Thanks.

dpx49470
dpx49470 改变!=到!==没有解决问题。您能否根据我提供给我的代码编写<p>标签中的渲染方法?
11 个月之前 回复
douxingmou4533
douxingmou4533 罪魁祸首是导致呈现格式错误的HTML。尝试只渲染<p>标签中的罪魁祸首,看看你是否得到了同样的错误。还建议使用!==而不是!=进行比较。JS做了一些奇怪的屁股类型比较。
11 个月之前 回复
douweng3383
douweng3383 我不使用Vue。我甚至试图尝试命令npmrunserve和composerdump-autoload无济于事。
11 个月之前 回复
doudiza9154
doudiza9154 该错误与javascript有关,而不是您发布的内容。你的模板中有Vue组件吗?
11 个月之前 回复

1个回答

I found the solution by changing the code in blade template from:

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

To:

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

The culprit then is the "?:", maybe not compliant with HTML5?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐