java - 在jsp中使用模态确认删除选定的项目
问题描述
我有一张桌子,上面有拒绝按钮。单击拒绝按钮后,它会打开一个模式弹出窗口以进行确认。我希望一旦用户确认弹出窗口,它应该删除该行。但是我的代码不起作用。它总是在 ajax 请求中显示未定义。
这是我的jsp代码...
`<table class="table table-bordered table-hover table-sm">
<thead>
<tr>
<th>User Profile ID</th>
<th>Name</th>
<th>Interested Received On</th>
<th>Accept</th>
<th>Reject</th>
<th>View Profile</th>
</tr>
</thead>
<tbody id="tableList">
</tbody>
<!-- REJECT MODEL POPUP -->
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Are you sure?</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Are you sure you want to reject this profile? You CAN NOT view this profile in your list anymore if you delete.
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type='button' class='btn btn-danger btn-sm btn-ok' id="action" onclick='rejectRequest(this)' data-toggle='modal' data-target='#rejectModel'>Yes, Reject</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">No, Don't Reject</button>
</div>
</div>
`
这是我的jQuery
function rejectRequest(link){
$('#rejectModel').show();
$('.loader').show ();
var url ="userHome"
var action =$(link).attr('href');
$.ajax({
url:url,
type:'Post',
data:{
action:action
},
success: function(result){
$('.loader').hide();
$('#confirm-reject').modal('hide');
var successUrl = "userHome.jsp";
window.location.href = successUrl;
},
error: function(jqxhr) {
$('.loader').hide();
$('#action').html("<font color='red'>Something went wrong. Please refresh and try again.</font>");
return false;
}
});
}
一旦用户单击是,拒绝,我想从 ajax 操作 var 中获取此值
每次我在 ajax 请求中获取 null 或未定义值时..请建议我哪里错了..
这是拒绝按钮 html
<button type="button" class="btn btn-danger btn-sm" name="accept" id="reject_cbd6631d-0c36-4332-8127-094dd5441984" value="Reject=cbd6631d-0c36-4332-8127-094dd5441984" data-href="Reject=cbd6631d-0c36-4332-8127-094dd5441984" data-toggle="modal" data-target="#confirm-reject">Reject</button>
提前致谢,感谢您的帮助。
解决方案
在您function
添加一个click
事件中,即单击拒绝按钮获取data-href
值,然后打开您modal
并将值传递给ajax
:
$("#confirm-reject").modal('hide');
var url = "userHome";
//on click of reject button
$('button[name="accept"]').on("click", function() {
//getting current value of button click
var action = $(this).attr('data-href');
$("#loader").show();
//opening modal
$("#confirm-reject").modal('show');
//on click of Yes,Reject button
$("#action").on("click", function() {
console.log(action);
$.ajax({
url: url,
type: 'Post',
data: {
action: action
},
success: function(result) {
$('.loader').hide();
$('#confirm-reject').modal('hide');
var successUrl = "userHome.jsp";
window.location.href = successUrl;
},
error: function(jqxhr) {
$('.loader').hide();
$('#action').html("<font color='red'>Something went wrong. Please refresh and try again.</font>");
return false;
}
});
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<form action="../UserHomeController" method="post" id="interestReceivedForm" class="needs-validation box text-secondary">
<h1 class="margin-top-h1">List of Interest Received</h1>
<div class="table-responsive" id="table-one">
<table class="table table-bordered table-hover table-sm">
<thead>
<tr>
<th>User Profile ID</th>
<th>Name</th>
<th>Interested Received On</th>
<th>Accept</th>
<th>Reject</th>
<th>View Profile</th>
</tr>
</thead>
<tbody id="tableList">
</tbody>
</table>
<div id="ignorecrossbefore" style="display:none;"> </div>
<div class="loader" id="loader">
<img src="../images/loader.gif" />
</div>
</div>
<!-- REJECT MODEL POPUP -->
<div class="modal cstm-model" id="confirm-reject" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Are you sure?</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Are you sure you want to reject this profile? You CAN NOT view this profile in your list anymore if you delete.
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type='button' class='btn btn-danger btn-sm btn-ok' id="action" data-toggle='modal' data-target='#rejectModel'>Yes, Reject</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">No, Don't Reject</button>
</div>
</div>
</div>
</div>
<button type="button" class="btn btn-danger btn-sm" name="accept" id="reject_cbd6631d-0c36-4332-8127-094dd5441984" value="Reject=cbd6631d-0c36-4332-8127-094dd5441984" data-href="Reject=cbd6631d-0c36-4332-8127-094dd5441984">Reject</button>
推荐阅读
- python - 无法卸载 Anaconda
- python - 我在这个接力计划计划中缺少什么?
- mysql - 查询 DISCRETE DECADE 列表的记录数
- javascript - 获取我的金额列的值并将其传递给我的文本框。(代码点火器)
- python - python if else表达式的不同求值结果
- ionic-framework - 如何解决此问题 - “无法解析所有参数”?
- python - Pytorch,如何扩展张量
- scala - 使用 Akka 流分区时,我得到“已经连接”
- c# - 如何修复 WPF 应用程序中的 TargetPlatformMinVersion 问题?
- html - Bootstrap 导航栏下拉菜单