在我的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
atrribute包含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为空))。当然,除非有可能在调用click()
函数时将ID传递给jQuery 。那可能吗?还有哪些其他选择?谢谢。
这应该工作:
$(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();
});
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句