javascript Jquery a href onclick 到控制器而无需重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18614261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery a href onclick to controller without reload page
提问by user2706372
I am trying to write a code that includes confirm message by using jquery.
我正在尝试使用 jquery 编写包含确认消息的代码。
?f ? click to Exit according to my below code occurs reload of my page.
?F ?根据我下面的代码单击退出会重新加载我的页面。
<a href="Exit" onclick="return confirm('Are you sure you want to exit?')">Exit</a>
How can ? write above code without reload page with confirm message in Jquery ?
怎么能 ?用 Jquery 中的确认消息在不重新加载页面的情况下编写上面的代码?
?f confirm ok = go to href (without reload) ?f confirm cancel = cancel it,
?f 确认确定 = 转到 href(无需重新加载) ?f 确认取消 = 取消它,
采纳答案by Ahsan Khurshid
Try the following code
试试下面的代码
<a href="#Exit" onclick="return confirm('Are you sure you want to exit?'); return false">Exit</a>
Explanation:
解释:
#
A URL fragment is a name preceded by a hash mark (#), which specifies an internal target location (an ID) within the current document.
#
URL 片段是一个以井号 (#) 开头的名称,它指定当前文档中的内部目标位置(一个 ID)。
While return false
is a way to tell the event to not actually fire.
Whilereturn false
是一种告诉事件实际上不会触发的方法。
回答by BENARD Patrick
I use this function to do something like that :
我使用这个函数来做这样的事情:
function confirm_text_url(message, url){
var response = confirm(message);
if (response==true)
{
document.location.href=url;
}
}
回答by hvgeertruy
You will have to evaluate the result
你将不得不评估结果
ex:
前任:
var r=confirm("Are you sure you want to exit?");
if (r==true)
{
x="You pressed OK!";
}
else
{
x="You pressed Cancel!";
}
in your case, it would be something like:
在你的情况下,它会是这样的:
<a href="javascript:void(0);" onclick="confirm('Are you sure you want to exit?') ? window.location = 'page-to-go-to': return ">Exit</a>
Also, use javascript:void(0); as the href, so you're sure the default propagation is not triggered
另外,使用 javascript:void(0); 作为 href,因此您确定不会触发默认传播
回答by Reinstate Monica Cellio
Your posted code will obviously already work, but since you've asked for it this is the same thing, but using jQuery to assign the event handler...
您发布的代码显然已经可以工作了,但是既然您已经要求了,这是同一件事,但是使用 jQuery 分配事件处理程序...
HTML
HTML
<a href="Exit" id="exit-link">Exit</a>
Javascript
Javascript
$(function() {
$("#exit-link").on("click", function() {
return confirm("Are you sure you want to exit?");
});
});
I added an ID to the link, just to be able to explicitly reference it using jQuery. Other than that it's the same thing.
我向链接添加了一个 ID,只是为了能够使用 jQuery 显式引用它。除此之外,它是一样的。
回答by user1859022
from the text in your confirm I guess you might be looking for this: How to show the "Are you sure you want to navigate away from this page?" when changes committed?
从您确认中的文本我猜您可能正在寻找这个: 如何显示“您确定要离开此页面吗?” 何时提交更改?
or dicussed in more detail regarding support for older browsers: How to show the "Are you sure you want to navigate away from this page?" when changes committed?
或更详细地讨论对旧浏览器的支持: 如何显示“您确定要离开此页面吗?” 何时提交更改?