我正在尝试向 Controller 提交一个模态,并执行一个@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>
最佳答案
编写 onlick 函数,它将设置路由到您的表单:
<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>
关于javascript - 尝试以 laravel 形式更新项目,但它发送的最后一个 id 值不是对应的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44056669/