admin管理员组文章数量:1336304
I am trying to write a code that includes confirm message by using jquery.
İf ı click to Exit according to my below code occurs reload of my page.
<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 ?
İf confirm ok = go to href (without reload) İf confirm cancel = cancel it,
I am trying to write a code that includes confirm message by using jquery.
İf ı click to Exit according to my below code occurs reload of my page.
<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 ?
İf confirm ok = go to href (without reload) İf confirm cancel = cancel it,
Share Improve this question edited Sep 4, 2013 at 12:58 Reinstate Monica Cellio 26.2k6 gold badges53 silver badges67 bronze badges asked Sep 4, 2013 at 12:47 user2706372user2706372 953 gold badges3 silver badges10 bronze badges 3- 1 That has nothing to do with jQuery. Apart from that, your code should just work. It will only reload if you hit 'ok'. – putvande Commented Sep 4, 2013 at 12:53
- @putvande Since he's asking how to do it using jQuery, please don't remove the jQuery tag. He's not saying the above code doesn't work. He's asked for an alternative. – Reinstate Monica Cellio Commented Sep 4, 2013 at 12:58
- 1 Fair enough. The the question was unclear. – putvande Commented Sep 4, 2013 at 13:00
5 Answers
Reset to default 1Try 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.
While return false
is a way to tell the event to not actually fire.
See Demo
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;
}
}
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...
HTML
<a href="Exit" id="exit-link">Exit</a>
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.
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 mitted?
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 mitted?
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
本文标签: javascriptJquery a href onclick to controller without reload pageStack Overflow
版权声明:本文标题:javascript - Jquery a href onclick to controller without reload page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742390656a2465920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论