-->

在回调函数调用的href(Calling href in Callback function)

2019-11-01 14:39发布

我在我的页面注销一个锚标记。

<a href="/logout/" id="lnk-log-out" />

我在这里表示相对于jQuery UI的对话框确认一个弹出。

如果用户单击是从对话框中,它具有执行链接按钮的默认操作,我的意思是HREF =“/注销”。

如果没有点击一个弹出框应消失。

jQuery代码

 $('#lnk-log-out').click(function (event) {
            event.preventDefault();
            var logOffDialog = $('#user-info-msg-dialog');

            logOffDialog.html("Are you sure, do you want to Logout?");
            logOffDialog.dialog({
                title: "Confirm Logout",
                height: 150,
                width: 500,
                bgiframe: true,
                modal: true,
                buttons: {
                    'Yes': function () {
                        $(this).dialog('close');
                        return true;
                    },
                    'No': function () {
                        $(this).dialog('close');
                        return false;
                    }
                }
            });

        });
    });

问题是我不能开火锚的href当用户点击YES。

我们应该怎么做?

编辑:现在,我以这种方式管理

'Yes': function () {
                        $(this).dialog('close');
                        window.location.href = $('#lnk-log-out').attr("href");
                    }

Answer 1:

在“是”被触发时调用的匿名函数,你要做到以下几点,而不是只返回true:

  1. 抓住HREF(你可以得到这个容易使用$('selector').attr('href');
  2. 执行你的window.location.href给你1点抓起网址

如果你想在a标签只是做它的东西,删除任何preventDefault()stopPropagation() 在这里,我已经提供了两种不同的方式:)

不要使用document.location ,使用window.location.href代替。 你可以看到为什么在这里 。

你在“是”呼叫代码应该是这个样子,你的代码插入课​​程:

'Yes': function () {
            // Get url
            var href = $('#lnk-log-out').attr('href');
            // Go to url
            window.location.href = href;
            return true; // Not needed
     }, ...

:由于安东尼在下面的意见:使用window.location.href = ...代替window.location.href()因为它不是一个功能!



Answer 2:

我在我的许多项目中使用这个,所以我建议window.location.href

'Yes': function () {
                        $(this).dialog('close');
                         window.location.href="your url"    
                        return true;
                    },


文章来源: Calling href in Callback function