如何解决laravel刀片选择选项正在全部选中
我试图将select t1.question_id,case when t1.element_id not like 'o_%' then t1.element_id else '' end element_id,case when t2.element_id like 'o_%' then t2.element_id else '' end element
from table t1
join table t2 on t1.question_id=t2.question_id
元素中的option
标记为已选中,但是它们都已被选中。
刀片HTML
select
如果我要创建新记录,那么 这是新记录 会打印在<h1>value = {{ $entity->field ?? 'this is new' }}</h1>
<select name="field">
<option value="option_a" @if ($entity->field ?? '' == 'option_a') selected="selected" @endif>Option A</option>
<option value="option_b" @if ($entity->field ?? '' == 'option_b') selected="selected" @endif>Option B</option>
<option value="option_c" @if ($entity->field ?? '' == 'option_c') selected="selected" @endif>Option C</option>
</select>
中。
如果我正在编辑现有记录,那么存储在数据库中的选项将打印在h1
中(假设为option_b)。
那为什么所有选项都具有h1
属性?
HTML输出
selected
解决方法
您似乎有逻辑问题。看来您要进行三元运算,然后决定进行标准的if-check,但是可能在代码中留下了三元运算的一些逻辑。
此部分:
@if ($entity->field ?? '' == 'option_a') selected="selected" @endif
似乎丢失了??
的另一端:
,因此总是求值为true
。
相反,请尝试:
@if ($entity->field == 'option_a') selected @endif
,
所选语法如下:
<option value="audi" selected>Audi</option>
所以尝试:
<h1>value = {{ $entity->field ?? 'this is new' }}</h1>
<select name="field">
<option value="option_a" @if ($entity->field ?? '' == 'option_a') selected @endif>Option A</option>
<option value="option_b" @if ($entity->field ?? '' == 'option_b') selected @endif>Option B</option>
<option value="option_c" @if ($entity->field ?? '' == 'option_c') selected @endif>Option C</option>
</select>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。