【发布时间】:2021-09-03 01:27:07
【问题描述】:
您好,我有一个带有输入数组的变量 例子: $userRestaurantsIds
array:2 [▼
0 => 12
1 => 13
]
作为这个 laravel 的结果,从 sql 查询的所有 id 中创建输入 我知道如何创建输入,但我需要禁用不在该数组中的值例如我知道如何创建输入但我需要禁用不在该数组中的值例如
<input value="13" id="tomorrow" type="checkbox" data-name="Loquillo Tienda 2" name="user_restaurants[]" checked="checked">
由于此输入的值为 13,因此启用了数组中的值
<input value="9" (input no clickeable or hidden but in the data for the submit) id="tomorrow" type="checkbox" data-name="Loquillo Tienda 2" name="user_restaurants[]" checked="checked">
但是这个输入的值为 9,它在检查中被启用,但它不能被编辑,因为它不在数组中。
我在 products.blade 页面上的输入查询是这样的
@foreach($allRestaurants as $ar)
<label>
<input id="tomorrow" type="checkbox" data-name="{{ $ar->name }}" name="user_restaurants[]" value="{{ $ar->id }}" @if(in_array($ar->id, $userRestaurantsIds)) checked="checked" @endif/>
<span>{{ $ar->name }}</span>
</label>
@endforeach
总之,我希望只有具有数组值的输入可见,其余的保持禁用或不可见,但它们在提交数据中
【问题讨论】:
-
这能回答你的问题吗? Insert a value to hidden input Laravel Blade
-
看起来您已经设法根据完全相同的标准设置
checked属性,使用@if(in_array($ar->id, $userRestaurantsIds)) checked="checked" @endif- 那么究竟是什么阻止您使用disabled做完全相反的事情属性……? -
“但它们在提交数据中” - 那么你肯定不想要
disabled,因为禁用的表单项不 导致表单提交数据集中的条目。然后使用readonly,或添加一个类以通过CSS隐藏它们。 (不过,未选中的复选框也不会在提交数据集中创建条目,因此除非您在开头设置checked属性,否则效果不会很大。) -
在 laravel 中相反的情况如何?会是这样吗? @if(!in_array($ar->id, $userRestaurantsIds)) 已检查="已检查" @endif
-
亲爱的@CBroe,如果我在 css 中添加一个类,用户可以进入控制台并使其可见并选择它们吗?这安全吗?