在我的Struts2应用程序中,我有一些按钮将从表中删除项。到现在为止还好。现在,我试图使用jQuery向用户提供一个警报,以确认他们是否真的想删除所选的项目。要做到这一点,我的想法是我认为这是最明显的方法:
2.1。如果用户按下“取消”,警报就会消失;
2.2。如果用户按下“确认”,该项目将被删除,然后警报消失;
到目前为止,我得到的是:
JSP:
<table>
<tr>
[...]
<td><a href="<s:url action="deleteexperiment"><s:param name="id"><s:property value="exp_id"/></s:param></s:url>" class="delete ink-button all-100">delete</a></td>
[...]
</tr>
</table>
<div id="confirm-delete" class="ink-alert basic" role="alert" style="display: none">
<p class="quarter-bottom-padding"><b>Confirm delete:</b> Do you really want to delete this experiment?</p>
<button id="no" class="ink-button all-20 red">Cancel</button>
<button id="yes" class="ink-button all-20 green">Confirm</button>
</div>
<script type="text/javascript">
$(document).ready(function()
{
$('.delete').click(function(e)
{
e.preventDefault();
$('#confirm-delete').show();
$('#yes').click(function(event)
{
// resume e.preventDefault(), i.e., continue redirecting to the original href on the table above
$('#confirm-delete').hide();
});
$('#no').click(function(event)
{
$('#confirm-delete').hide();
});
});
});
</script>我尝试了20种不同的方法(在试图恢复链接的原始行为方面--注释行),但没有结果。请注意,href属性包含Struts标记,所以像window.location.href = "<s:url action="deleteexperiment"><s:param name="id"><s:property value="exp_id"/></s:param></s:url>";这样的东西不能工作,因为我猜jQuery不会“知道”ID是什么(我猜它会重定向到http://localhost:8080/AppName/deleteexperiment.action?id=,所以ID是空的)。当然,除非在调用jQuery函数时可以将ID传递给click()。这有可能吗?还有其他选择留给我吗?谢谢。
发布于 2015-06-06 00:48:44
这应该是可行的:
$(document).ready(function()
{
$('.delete').click(function(e)
{
e.preventDefault();
var href = $(this).attr('href'); // store the href attr
$('#confirm-delete').show();
$('#yes').click(function(event)
{
// resume e.preventDefault(), i.e., continue redirecting to the original href on the table above
window.location.href = href; // redirect with stored href
$('#confirm-delete').hide();
});
$('#no').click(function(event)
{
$('#confirm-delete').hide();
});
});
});https://stackoverflow.com/questions/30677333
复制相似问题