简体   繁体   English

如何在单击提交按钮时显示弹出窗口

[英]How to show a popup window on submit button click

I have a html form where the user fills an input with an integer. 我有一个html表单,其中用户用整数填充输入。

<input id="helpBlock" type="number" min="0" step="1" class="form-control" name="quantity" required>

What I want to do is when the user submits the form, compare the input with a number and if a condition is met then a popup window to show and ask for confirmation before the submit continues. 我想做的是,当用户提交表单时,将输入与数字进行比较,如果满足条件,则将显示一个弹出窗口,并在继续提交之前要求确认。

 $('form').on('submit', function(e) { e.preventDefault(); var quantity = $('#helpBlock').val(); // Read the user input var quantityW9 = 100; //the number to compare if (quantity > quantityW9) { console.log("quantity is bigger -> " + quantity); //Here show a confirmation window in order to continue } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="row"> <div class="col-md-12"> <form id="form" method="post" class="form-horizontal" role="form"> <div class="has-warning"> <label for="inputWarning1" class="col-md-1 control-label">Quantity</label> <div class="col-md-2"> <input id="helpBlock" type="number" min="0" step="1" class="form-control" name="quantity" required> </div> </div> <div class="form-group"> <div class="col-lg-10 col-lg-offset-2"> <button type="submit" name="formAction" class="btn btn-success">Submit</button> </div> </div> </form> </div> </div> 

See below snippet. 请参见下面的代码段。

 $('form').on('submit', function(e) { e.preventDefault(); var quantity = $('#helpBlock').val(); // Read the user input var quantityW9 = 100; //the number to compare var result = confirm("you want to continue"); console.log(result); if(result){ if (quantity > quantityW9) { console.log("quantity is bigger -> " + quantity); //Here show a confirmation window in order to continue } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="row"> <div class="col-md-12"> <form id="form" method="post" class="form-horizontal" role="form"> <div class="has-warning"> <label for="inputWarning1" class="col-md-1 control-label">Quantity</label> <div class="col-md-2"> <input id="helpBlock" type="number" min="0" step="1" class="form-control" name="quantity" required> </div> </div> <div class="form-group"> <div class="col-lg-10 col-lg-offset-2"> <button type="submit" name="formAction" class="btn btn-success">Submit</button> </div> </div> </form> </div> </div> 

Now the confirmation window only pops up if the condition (integer comparison) is true . 现在,仅在条件(整数比较)为true时弹出确认窗口。 And we also get his decision in the confirmation window: OK or Cancel so we can handle each case accordingly: 我们还会在确认窗口中得到他的决定:“ 确定”或“取消”,因此我们可以相应地处理每种情况:

 var can_submit = false; $('form').on('submit', function(e) { if(can_submit){ /*---START: ONLY FOR DEMONSTRATION PURPOSE---*/ e.preventDefault(); console.log("submitted now!"); /*---END: ONLY FOR DEMONSTRATION PURPOSE---*/ } else { e.preventDefault(); var quantity = $('#helpBlock').val(); // Read the user input var quantityW9 = 100; //the number to compare if (quantity > quantityW9) { console.log("quantity is bigger -> " + quantity); var confirmation = confirm("Do you want to continue"); if(confirmation){ console.log("Clicked OK - submitting now ..."); can_submit = true; $('form').submit(); } else { console.log("Clicked Cancel"); can_submit = false; } } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="row"> <div class="col-md-12"> <form id="form" method="post" class="form-horizontal" role="form"> <div class="has-warning"> <label for="inputWarning1" class="col-md-1 control-label">Quantity</label> <div class="col-md-2"> <input id="helpBlock" type="number" min="0" step="1" class="form-control" name="quantity" required> </div> </div> <div class="form-group"> <div class="col-lg-10 col-lg-offset-2"> <button type="submit" name="formAction" class="btn btn-success">Submit</button> </div> </div> </form> </div> </div> 

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

相关问题 如何在 chrome 中单击弹出窗口 window 中的特定按钮 - How to click on a specific button in a popup window in chrome 如何访问弹出窗口并单击按钮? - How to access and click button of popup window? 在提交按钮上打开一个弹出窗口 - Open a popup window on submit button 如何在弹出窗口中的提交按钮上设置默认焦点 window - how to set default focus on the submit button in a popup window 表单:弹出窗口中的预览按钮 window,无弹出窗口的提交按钮 window - Form: preview button in popup window, submit button without popup window 单击按钮时如何打开使用Ajax提交的弹出表单? - How to open popup form submit with Ajax when click a button? 如何在按钮单击事件上将视图加载为Kendo窗口弹出窗口 - How to load a View as Kendo Window popup on Button Click Event 如何在window.open中打开一个按钮查看视图,并在angularJs中作为弹出窗口打开 - How to open a View on button click in window.open as a popup in angularJs 单击提交后如何隐藏内容(如文本,单选按钮和提交按钮)弹出窗口并在同一弹出窗口中显示新内容? - How to hide content(like text, radio button, and submit button) Popup window after Clicking on submit and display new content in the same popup? 提交完成后如何显示div而不是单击按钮? - How to show div after Submit is complet instead click button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM