最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

alert - Javascript cancel button is submitting form - Stack Overflow

programmeradmin2浏览0评论

I am trying to add a javascript alert to my form, the problem I am having is that when the user clicks cancel, the form still gets submitted?.

In the head I have :

<head>
<script type="text/javascript">
function confirmDelete() {if(confirm('Are you sure you want to delete this category ?'))
alert('Category Deleted !');
else alert('Cancelled !')}
</script>
</head>

My link uses the following :

a onClick="confirmDelete()"

Im not sure why clicking cancel still submits it?, is it possible to show Cancelled ! for 2 seconds and then close also ?.

I am trying to add a javascript alert to my form, the problem I am having is that when the user clicks cancel, the form still gets submitted?.

In the head I have :

<head>
<script type="text/javascript">
function confirmDelete() {if(confirm('Are you sure you want to delete this category ?'))
alert('Category Deleted !');
else alert('Cancelled !')}
</script>
</head>

My link uses the following :

a onClick="confirmDelete()"

Im not sure why clicking cancel still submits it?, is it possible to show Cancelled ! for 2 seconds and then close also ?.

Share Improve this question asked Nov 26, 2012 at 11:13 Iain SimpsonIain Simpson 8,14113 gold badges49 silver badges67 bronze badges 1
  • 1 This should help you: stackoverflow./questions/932653/… – pabrantes Commented Nov 26, 2012 at 11:15
Add a ment  | 

3 Answers 3

Reset to default 4

try using the cancel

function confirmDelete(e) {
 if(confirm('Are you sure you want to delete this category ?'))
   alert('Category Deleted !');
 else {
  alert('Cancelled !');
  e.preventDefault();
 }
}

and change your onclick to

onclick="confirmDelete(event)"

You need to return the result from the confirm:

function confirmDelete() {
    return confirm('Are you sure you want to delete this category ?');
}


onClick="confirmDelete()"

you can use jQuery click event and inside event put e.preventDefault(); and it will prevent default and default is submiting form-

发布评论

评论列表(0)

  1. 暂无评论