仅当输入与laravel中的id匹配时才启用输入



你好,我有一个带有输入数组的变量示例:$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,它在检查中启用,但无法编辑,因为它不在数组中。

我对产品的输入查询。叶片页面是这个

@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

总之,我希望只有具有数组值的输入是可见的,其余的保持禁用或不可见,但它们在提交数据中

使用if语句检查您的值是否在数组中。如果它不在数组中,请将display设置为none。这将出现在结果数据中,除非有人编辑DOM。

<input value="9" id="tomorrow" type="checkbox" data-name="Loquillo Tienda 2" name="user_restaurants[]" checked="checked" @if(! in_array(9, $userRestaurantsIds)) style="display:none;" @endif>

第二种方法是隐藏输入

@if(! in_array(9, $userRestaurantsIds)) 
<input type="hidden" value="9" name="user_restaurants[]">
@endif>

如果使用第一个选项,请确保它具有checked属性,否则它不会出现在数据中。

最新更新