尝试更新 laravel 形式的项目,但它发送的最后一个 ID 值不是对应的 ID
trying to update an item in laravel form but it sends last value of id not corresponding id
我正在尝试向控制器提交一个模态,并执行一个@foreach 循环,但是当我尝试发送相应项目 ID 的 ID 时,它发送 ID 的最后一个值,总是 = 13: -
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($categories as $category)
<tr>
<td>{{ $category->id }}</td>
<td>{{ $category->name }}</td>
<td>
<button class="myBtn btn btn-primary">Edit</button>
<button class="btn btn-danger">Delete</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
所以当我按下编辑按钮时,它总是发送 id 的最后一个值而不是相应的值
<form action="{{ route('category.update', $category->id) }}" method="post">
<div>
<label for="name">Name:</label>
<input type="text" id="Category Name" name="name">
</div>
<button type="submit" class="bn btn-success">Save</button>
{{ csrf_field() }}
</form>
编写 onclick 函数,它将设置路由到您的表单:
<button data-url="{{ route('category.update', $category->id) }}" class="myBtn btn btn-primary" onclick="changeRoute({{ route('category.update', $category->id) }})">Edit</button>
在你的脚本中写这个,但是你需要给你的表单 id myForm
<script>
function changeRoute(url) {
alert(url);
$("#myForm").attr("action",url);
}
$(".myBtn").click(function() {
var url = $(this).attr("data-url");
console.log(url);
$("#myForm").attr("action",url);
});
</script>
我正在尝试向控制器提交一个模态,并执行一个@foreach 循环,但是当我尝试发送相应项目 ID 的 ID 时,它发送 ID 的最后一个值,总是 = 13: -
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($categories as $category)
<tr>
<td>{{ $category->id }}</td>
<td>{{ $category->name }}</td>
<td>
<button class="myBtn btn btn-primary">Edit</button>
<button class="btn btn-danger">Delete</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
所以当我按下编辑按钮时,它总是发送 id 的最后一个值而不是相应的值
<form action="{{ route('category.update', $category->id) }}" method="post">
<div>
<label for="name">Name:</label>
<input type="text" id="Category Name" name="name">
</div>
<button type="submit" class="bn btn-success">Save</button>
{{ csrf_field() }}
</form>
编写 onclick 函数,它将设置路由到您的表单:
<button data-url="{{ route('category.update', $category->id) }}" class="myBtn btn btn-primary" onclick="changeRoute({{ route('category.update', $category->id) }})">Edit</button>
在你的脚本中写这个,但是你需要给你的表单 id myForm
<script>
function changeRoute(url) {
alert(url);
$("#myForm").attr("action",url);
}
$(".myBtn").click(function() {
var url = $(this).attr("data-url");
console.log(url);
$("#myForm").attr("action",url);
});
</script>