如何在jQuery Confirmation插件点击确认按钮后提交表单?
解决方案:点击确认按钮提交表单
我来帮你搞定这个问题!要在点击「确认」按钮时提交表单,你只需要在Confirm按钮的配置里添加回调函数,在函数中触发表单的提交操作即可。这里有两种实用的实现方式,你可以根据自己的页面场景选择:
方式1:通过按钮定位父表单提交(通用场景)
如果你的删除按钮是嵌套在表单内部的,这种方式最通用,不需要依赖表单ID就能工作:
$(function() { $("button[name='btn-delete-akun']").confirm({ title: '<p style="color:red;"> <span class="glyphicon glyphicon-warning-sign" aria-hidden="true"></span> Peringatan!<p>', content: 'Apakah anda yakin ingin menghapus event? ', useBootstrap: true, buttons: { Confirm: { btnClass: 'btn-red', action: function() { // 找到当前按钮所在的表单并提交 this.$target.closest('form').submit(); } }, Cancel: function() {} } }); });
方式2:通过表单ID直接提交(指定场景)
如果你的表单有明确的ID(比如id="delete-event-form"),可以直接通过ID定位表单提交,代码更直观:
$(function() { $("button[name='btn-delete-akun']").confirm({ title: '<p style="color:red;"> <span class="glyphicon glyphicon-warning-sign" aria-hidden="true"></span> Peringatan!<p>', content: 'Apakah anda yakin ingin menghapus event? ', useBootstrap: true, buttons: { Confirm: { btnClass: 'btn-red', action: function() { // 通过ID提交指定表单 $('#delete-event-form').submit(); } }, Cancel: function() {} } }); });
额外说明:
this.$target指向的是触发确认框的按钮(也就是你的btn-delete-akun按钮),用closest('form')能快速找到它所在的父表单。- 如果你的需求是AJAX异步提交(不想刷新页面),也可以把
submit()换成AJAX请求代码,比如:$.ajax({ url: '/your-delete-api-url', method: 'POST', data: $('#delete-event-form').serialize(), success: function(response) { // 处理成功逻辑,比如提示删除成功或刷新页面 alert('Event berhasil dihapus!'); location.reload(); } });
内容的提问来源于stack exchange,提问作者hfbachri_
相关产品推荐
相关产品推荐

