嗨,当我从多个复选框中取消选中值时,我遇到了问题
当我从循环中取消选中多个复选框中的一个复选框/从复选框中获取foreach值时,仍显示/不删除(提交给控制器时)
例如,当我取消选中值G000时,值G000仍显示/不删除
这是我的代码
@extends('layouts.master')
@section('title', 'Edit User Module')
@push('style')
<link rel="stylesheet" type="text/css" href="http://www.shieldui.com/shared/components/latest/css/light-bootstrap/all.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">
@endpush
@section('content')
<div class="card">
<form action="{{ route('module-users.update.route', $user_id) }}" method="POST">
@csrf
<div class="card-header">Edit User Module</div>
<div class="card-body">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<ul id="treeview">
@foreach ($parent_menus as $parent_menu)
{{-- <li data-expanded="true">{{ $parent_menu->Description }} {{ $parent_menu->ModuleID }} --}}
<li>{{ $parent_menu->Description }} {{ $parent_menu->ModuleID }}
<input type="checkbox" name="menus_id[]" class="pc-box"
value="{{ $parent_menu->ModuleID }}" onclick="check_parent(this)"
{{ in_array(trim($parent_menu->ModuleID), $all_modules) ? 'checked' : '' }}
{{-- onclick="$(this).val(this.checked ? '{{ $parent_menu->ModuleID }}' : 0);" --}}
>
<ul>
@foreach ($child_menus1 as $child_menu1)
@if ($parent_menu->ModuleID == $child_menu1->HeaderId)
<li data-expanded="true">{{ $child_menu1->Description }} {{ $child_menu1->ModuleID }}
<input type="checkbox" name="menus_id[]" value="{{ $child_menu1->ModuleID }}"
class="cc-box-{{ $parent_menu->ModuleID }}"
{{ in_array(trim($child_menu1->ModuleID), $all_modules) ? 'checked' : '' }}
{{-- onclick="$(this).val(this.checked ? '{{ $child_menu1->ModuleID }}' : 0);" --}}
>
<ul>
@foreach ($child_menus2 as $child_menu2)
@if ($child_menu1->ModuleID == $child_menu2->HeaderId)
<li>{{ $child_menu2->Description }} {{ $child_menu2->ModuleID }}
<input type="checkbox" name="menus_id[]" value="{{ $child_menu2->ModuleID }}"
class="cc-box-{{ $parent_menu->ModuleID }}"
{{ in_array(trim($child_menu2->ModuleID), $all_modules) ? 'checked' : '' }}
{{-- onclick="$(this).val(this.checked ? '{{ $child_menu1->ModuleID }}' : 0);" --}}
>
</li>
@endif
@endforeach
</ul>
</li>
@endif
@endforeach
</ul>
</li>
@endforeach
</ul>
</div>
</div>
<input type="hidden" name="user_id" value="{{ $user_id }}">
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</form>
</div>
@endsection
@push('script')
<script type="text/javascript" src="http://www.shieldui.com/shared/components/latest/js/shieldui-all.min.js"></script>
<script type="text/javascript">
jQuery(function ($) {
$("#treeview").shieldTreeView();
});
$(function() {
$('.attribute').click(function() {
alert($(this).val() + ' ' + (this.checked ? 'checked' : 'unchecked'));
});
});
function check_parent(e){
$(".cc-box-"+e.value).attr("checked", "true");
}
</script>
@endpush
这是我从控制器更新的方法
public function update_route(Request $request, $user_id)
{
$user_id = $request->user_id;
$menus_id = $request->menus_id;
$menus = array_unique($menus_id);
foreach ($menus as $menu) {
$check = DB::table('usermodules')
->where('moduleid', $menu)
->where('userid', 'temmy')
->exists();
if(!$check)
{
UserModule::create([
'userid' => 'temmy',
'moduleid' => $menu,
'r_read' => '1',
'r_edit' => '1',
'r_delete' => '1',
'fou' => '1',
'fop' => '1',
'fod' => '1',
]);
}else{
DB::table('usermodules')->where('userid', 'temmy')->delete();
UserModule::create([
'userid' => 'temmy',
'moduleid' => $menu,
'r_read' => '1',
'r_edit' => '1',
'r_delete' => '1',
'fou' => '1',
'fop' => '1',
'fod' => '1',
]);
}
}
return redirect()->back();
}
这是我的UserModule
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserModule extends Model
{
// Custom set name table
protected $table = 'usermodules';
// Custon set primary key name
protected $primaryKey = 'userid';
// Off created_at and updated_at built in
public $timestamps = false;
// Off incrementing built in
public $incrementing = false;
protected $fillable = ['userid', 'moduleid', 'r_read', 'r_edit', 'r_delete', 'fou', 'fop', 'fod'];
}
这个问题有解决方案吗?
谢谢
[UI屏幕截图][1]