javascript确认(取消)还是提交表单时使用return false? [英] javascript confirm(cancel) still submits form when using return false?

查看:142
本文介绍了javascript确认(取消)还是提交表单时使用return false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

 < form method ='POST'action ='post.php'style = '边距:0像素;' > 
< input type ='image'name ='close_x'src ='imgs / button.png'onclick ='confirmClose()'>
< / form>

函数confirmClose(){
if(confirm('你确定要关闭吗?')){
return true;
} else {
return false;


$ / code>

但是,当您在确认框中单击取消时,它仍然提交表格。



肯定返回false应该会阻止这种情况发生吗?

任何想法为什么?



谢谢。

解决方案不要使用内联事件处理程序。



  document.getElementsByName('close_x')[0] .onclick = function confirmClose(e){if(!confirm('Are you sure '))e.preventDefault();};  

  #myform {margin-bottom:0px;}  

< form id =myformmethod ='POST'action ='post.php'> < input type ='image'name ='close_x'src ='imgs / button.png'>< / form>


i am using the following code:

        <form method='POST' action='post.php' style='margin-bottom:0px;'>
        <input type='image' name='close_x' src='imgs/button.png' onclick='confirmClose()'>
        </form>

       function confirmClose() {
            if (confirm('Are you sure you wish to close?')) {
                return true;
            } else {
                return false;
            }
        }

however, when you click cancel in the confirm box, it still submits the form.

Surely the return false should stop this from happening?

Any idea why?

Thanks.

解决方案

Don't use inline event handlers.

document.getElementsByName('close_x')[0].onclick = function confirmClose(e) {
  if (!confirm('Are you sure you wish to close?'))
    e.preventDefault();
};

#myform {
  margin-bottom: 0px;
}

<form id="myform" method='POST' action='post.php'>
  <input type='image' name='close_x' src='imgs/button.png'>
</form>

这篇关于javascript确认(取消)还是提交表单时使用return false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆