简体   繁体   English

如何在 EJS 模板中的表单发布请求之前出现确认对话框

[英]How to make a Confirm Dialog Appear before Form Post Request in EJS template

I'm trying to create a confirmation dialog using something like this: confirm('Are you sure?') inside of my EJS form but I don't know where and how to get it to work correctly.我正在尝试使用以下内容创建一个确认对话框: confirm('Are you sure?')在我的 EJS 表单中,但我不知道在哪里以及如何让它正常工作。

Here is the code I have so far for my EJS template:这是到目前为止我的 EJS 模板的代码:

<div id="popupAddUser" class="addUser">
    <div class="card h-100">
        <div class="row">
            <div class="col-7"><h2>Create New User</h2></div>
        </div>
        <div class="card-body">
            <form method="POST" id="formAddUser" action="/add-user" enctype="multipart/form-data">
                <div class="container">
                    <div class="row">
                        <div class="col-xl-5 float-right">
                            <div class="form-group">
                                <input class="form-control" type="text" placeholder="Type a User name"
                                       name="create_user">
                            </div>
                            <div>
                                <button type="submit" class="btn btn-primary float-right" form="formAddUser" onclick="checker()">Create
                                    User
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    function checker(){
        if( confirm("Are you sure you want to create a new User in the database?")){
            continue;
        }else{
            break;
        }   
    }
</script>

I'm aware that the checker() function isn't correctly placed or even necessary but I'm not sure if there's another way to do this.我知道 checker() function 放置不正确,甚至没有必要,但我不确定是否还有其他方法可以做到这一点。 When I click the create user button, the Confirm dialog box should render and ask me if I'm sure I want to create a user.当我单击创建用户按钮时,应呈现确认对话框并询问我是否确定要创建用户。 If I select yes, it should submit the form but if I click cancel, it shouldn't do the form post request如果我 select 是的,它应该提交表单但是如果我点击取消,它不应该做表单发布请求

You can use the JavaScript submit() function to remotely submit your form depending on the response.您可以使用 JavaScript submit() function 根据响应远程提交您的表单。 This would mean that instead of using a submit button, you'd have to use a normal button.这意味着您必须使用普通按钮,而不是使用提交按钮。

 function checker(){ if(window.confirm("Are you sure")){ document.getElementById("formAddUser").submit(); } else { return; } }
 <div id="popupAddUser" class="addUser"> <div class="card h-100"> <div class="row"> <div class="col-7"><h2>Create New User</h2></div> </div> <div class="card-body"> <form method="POST" id="formAddUser" action="/add-user" enctype="multipart/form-data"> <div class="container"> <div class="row"> <div class="col-xl-5 float-right"> <div class="form-group"> <input class="form-control" type="text" placeholder="Type a User name" name="create_user"> </div> <div> <button type="button" class="btn btn-primary float-right" form="formAddUser" onclick="checker()">Create User </button> </div> </div> </div> </div> </form> </div> </div> </div>

Note that the button type is set to button rather than submit to prevent the page from refreshing after the function runs.注意按钮类型设置为button而不是submit ,以防止function运行后页面刷新。

I am surprised your text editor did not complain about the continue and break statement in your if-else statement:!我很惊讶您的文本编辑器没有抱怨 if-else 语句中的 continue 和 break 语句:! I would:我会:

  • Remove the inline onclick event handler删除内联 onclick 事件处理程序

  • Use instead the addEventListener() method or the onsubmit call on the form element.改用 addEventListener() 方法或表单元素上的 onsubmit 调用。

  • Use event.preventDefault() method to stop the form from submitting when the cancel button is clicked.单击取消按钮时,使用 event.preventDefault() 方法阻止表单提交。

     document.getElementById('form').addEventListener( 'submit', (e) => checker(e) ) function checker(e) { if(?confirm('Are you sure.')) { e.preventDefault() alert('Form was not submitted') }else { alert('New user created') } }

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

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