admin管理员组文章数量:1397192
I'm trying to stop the default action when a link is clicked. Then I ask for confirmation and if confirmed I want to continue the event. How do I do this? I can stop the event but can't start it. Here's what I have so far:
$(document).ready(function(){
$(".del").click(function(event) {
event.preventDefault();
if (confirm('Are you sure to delete this?')) {
if (event.isDefaultPrevented()) {
//let the event fire. how?
}
}
});
});
I'm trying to stop the default action when a link is clicked. Then I ask for confirmation and if confirmed I want to continue the event. How do I do this? I can stop the event but can't start it. Here's what I have so far:
$(document).ready(function(){
$(".del").click(function(event) {
event.preventDefault();
if (confirm('Are you sure to delete this?')) {
if (event.isDefaultPrevented()) {
//let the event fire. how?
}
}
});
});
Share
Improve this question
edited Jan 26, 2010 at 0:19
cletus
626k169 gold badges919 silver badges945 bronze badges
asked Jan 25, 2010 at 8:22
vagabondvagabond
1,7274 gold badges19 silver badges21 bronze badges
1
-
Just doing this myself, and found this question. I solved this by putting
return true
in my function :) – David Yell Commented Aug 9, 2010 at 10:25
2 Answers
Reset to default 4There's no need to prevent default to start. Just do this:
$(function() {
$(".del").click(function(evt) {
if (!confirm("Are you sure you want to delete this?")) {
evt.preventDefault();
}
});
});
It's easier and more logical to prevent the event once you need to rather than preventing it and then un-preventing it (if that's even possible).
Remember that the code will stop running when the confirm box is presented to the user until the user selects OK or Cancel.
By the way, take a look at JavaScript: event.preventDefault() vs return false. Depending on if you want to stop the event propagation or not you may want to either call stopPropagation()
or return false
:
$(function() {
$(".del").click(function(evt) {
if (!confirm("Are you sure you want to delete this?")) {
return false;
}
});
});
Much better to just return the confirm()
$(function() {
$(".del").click(function() {
return confirm("Are you sure you want to delete this?");
});
});
本文标签: javascriptHow do you stop and then starttrigger an event with JQueryStack Overflow
版权声明:本文标题:javascript - How do you stop and then starttrigger an event with JQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744150080a2593022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论