我有以下代码:
$('a.confirm').click(function(e) { e.preventDefault(); var answer = confirm("Are you sure?") if (answer){ // do the default action } });
解决方法
$('a.confirm').click(function(e) { var answer = confirm("Are you sure?") if (answer){ // do the default action } else { e.preventDefault(); } });
要么
$('a.confirm').click(function(e) { var answer = confirm("Are you sure?") if (!answer){ e.preventDefault(); } });
甚至只是
$('a.confirm').click(function(e) { return confirm("Are you sure?"); });