首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Struts2 / jQuery:简历链接href在e.preventDefault()之后重定向;

Struts2 / jQuery:简历链接href在e.preventDefault()之后重定向;
EN

Stack Overflow用户
提问于 2015-06-05 23:38:27
回答 1查看 1.2K关注 0票数 3

在我的Struts2应用程序中,我有一些按钮将从表中删除项。到现在为止还好。现在,我试图使用jQuery向用户提供一个警报,以确认他们是否真的想删除所选的项目。要做到这一点,我的想法是我认为这是最明显的方法:

  1. 用户点击“删除”按钮;
  2. 向用户显示警告(它包含两个链接:一个用于取消删除操作,另一个用于确认);

2.1。如果用户按下“取消”,警报就会消失;

2.2。如果用户按下“确认”,该项目将被删除,然后警报消失;

到目前为止,我得到的是:

JSP:

代码语言:javascript
复制
<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()。这有可能吗?还有其他选择留给我吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-06 00:48:44

这应该是可行的:

代码语言:javascript
复制
$(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();
        });
    });
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30677333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档