【发布时间】:2020-07-07 20:37:54
【问题描述】:
我在将数据传输到我的删除确认模式时遇到问题。 我已经验证了我的删除路线可以从数据库中删除数据,但我面临的问题是我无法将联系人-> id 传递到模态以访问以进行删除。
模态
<!-- Delete Warning Modal -->
<div class="modal modal-danger fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="Delete" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Contact</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{ route('contacts.destroy', 'id') }}" method="post">
@csrf
@method('DELETE')
<input id="id" name="id")>
<h5 class="text-center">Are you sure you want to delete this contact?</h5>
<input id="firstName" name="firstName"><input id="lastName" name="lastName">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-sm btn-danger">Yes, Delete Contact</button>
</div>
</form>
</div>
</div>
</div>
<!-- End Delete Modal -->
刀锋呼唤
<td>
<a href="#"
data-id={{$value->id}}
class="btn btn-danger delete"
data-toggle="modal"
data-target="#deleteModal">Delete</a>
</td>
联系控制器
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
// Need to find all addresses with the contacdt Id and delete them.
Address::where('contact_id', $id)->delete();
Contact::find($id)->delete();
return redirect()->route('contacts.index')->with('success','Contact deleted success');
}
总而言之,我的问题是让 jQuery 将数据传输到模态,这样我就可以使用它来删除数据......现在我的 id=null
【问题讨论】:
-
您没有在表单中发送
id值 -
这很简单。首先,您在 jquery 中获取点击事件的 id,然后将 id 作为目标输入字段中的值(在您的情况下为 $('#id').val() ),然后您将获得确切的结果。
标签: php jquery laravel modal-dialog