如果是旧值,则选择选择框的旧值,否则选择来自控制器的值
我在编辑表单中有一个选择框,一切正常,但问题是当存在旧值时,它总是从数据库中选择值。我认为问题在于循环索引。当我选择低于原始值的选项时,它会选择旧值,但当选择高于所选值的选项时,它会保留初始值。
<select class="form-control
searchableSelect {{ $errors -> has('Cash_expense_Account') ? 'is-invalid' : '' }}"
name="Cash_expense_Account">
<option value="">Search...</option>
@for($i = 0; $i < count($Cash_Accounts); $i++)
<option value="{{ $Cash_Accounts[$i]['id'] }}"
{{ ( $Cash_Accounts[$i]['id'] == $expense->coa_sub_account_id) ? 'selected' : '' }}
{{ old('Cash_expense_Account') == $Cash_Accounts[$i]['id'] ? 'selected' : '' }}>
{{ $Cash_Accounts[$i]['acc_sub_name'] }}
</option>
@endfor
</select>
I have a select box in the edit form, and everything is working fine, but the problem is when there is an old value, it always selects the value from the database. I think the issue is loop indexing. When I choose an option down from the original value, it selects the old value, but it retains the initial value when selecting an option above the selected value.
<select class="form-control
searchableSelect {{ $errors -> has('Cash_expense_Account') ? 'is-invalid' : '' }}"
name="Cash_expense_Account">
<option value="">Search...</option>
@for($i = 0; $i < count($Cash_Accounts); $i++)
<option value="{{ $Cash_Accounts[$i]['id'] }}"
{{ ( $Cash_Accounts[$i]['id'] == $expense->coa_sub_account_id) ? 'selected' : '' }}
{{ old('Cash_expense_Account') == $Cash_Accounts[$i]['id'] ? 'selected' : '' }}>
{{ $Cash_Accounts[$i]['acc_sub_name'] }}
</option>
@endfor
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将比较限制为仅使用旧值(如果存在),例如:
??
运算符检查旧值是否存在,否则使用循环中的旧值。Limit the comparison to only use old value if it exists, like:
the
??
operator checks if old value exists otherwise use one from the loop.