在回调函数中调用href [英] Calling href in Callback function
问题描述
我的页面上有一个锚定标签以供注销.
<a href="/logout/" id="lnk-log-out" />
在这里,我正在显示一个弹出窗口,用于通过jQuery UI对话框进行确认.
如果用户在对话框中单击是",则必须执行链接按钮的默认操作,即href ="/logout".
如果没有单击,弹出框应消失.
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': function () {
$(this).dialog('close');
window.location.href = $('#lnk-log-out').attr("href");
}
在触发是"时调用的匿名函数中,您想要执行以下操作,而不仅仅是返回true:
- 获取href(您可以使用
$('selector').attr('href');
轻松获取此信息) - 对您在第1点抓取的网址执行
window.location.href
如果您只想让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
}, ...
注意:感谢Anthony在下面的评论中:使用window.location.href = ...
代替window.location.href()
,因为它不是不是功能!
I have a anchor tag in my page for logout.
<a href="/logout/" id="lnk-log-out" />
Here I am showing a Popup for confirmation with jQuery UI dialog.
If user click Yes from dialog it has to execute the link button's default action, I mean href="/logout".
If No clicked a Popup box should be disappeared.
jQuery Code
$('#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;
}
}
});
});
});
The problem is I am not able to fire anchor's href when User click YES.
How can we do this?
Edit: Right now I managed in this way
'Yes': function () {
$(this).dialog('close');
window.location.href = $('#lnk-log-out').attr("href");
}
In the anonymous function called when 'Yes' is fired, you want to do the following instead of just returning true:
- Grab the href (you can get this easily using
$('selector').attr('href');
) - Perform your
window.location.href
to the url you grabbed in point 1
If you want the a
tag to just do it's stuff, remove any preventDefault()
or stopPropagation()
. Here I have provided two different ways :)
Don't use document.location
, use window.location.href
instead. You can see why here.
Your code in the 'Yes' call should look something like, with your code inserted of course:
'Yes': function () {
// Get url
var href = $('#lnk-log-out').attr('href');
// Go to url
window.location.href = href;
return true; // Not needed
}, ...
Note: Thanks to Anthony in the comments below: use window.location.href = ...
instead of window.location.href()
, because it's not a function!
这篇关于在回调函数中调用href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!