简体   繁体   English

javascript确认(取消)仍在使用return false时提交表单?

[英]javascript confirm(cancel) still submits form when using return false?

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? 当然,返回false应该阻止这种情况发生吗?

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM