【问题标题】:Show Confirm Alert Box before submitting the PHP Form在提交 PHP 表单之前显示确认警报框
【发布时间】:2018-03-27 20:13:10
【问题描述】:
<script type="text/javascript">

var validate = (function() {
  var reClass = /(^|\s)required(\s|$)/;  // Field is required
  var reValue = /^\s*$/;                 // Match all whitespace


  return function (form) {
    var elements = form.elements;
    var el;
    var amount = document.forms["myForm"]["amount"].value;
    var quantity = document.forms["myForm"]["quantity"].value;

    for (var i=0, iLen=elements.length; i<iLen; i++) {
      el = elements[i];

      if (reClass.test(el.className) && reValue.test(el.value)) {
        // Required field has no value or only whitespace
        // Advise user to fix
        alert('Please fix ' + el.name);
        return false;
      }

    }
    if (amount == null || amount == "" || isNaN(amount)) {
        alert("Total Amount must be only numbers");
        return false;
    } 
    else if (quantity == null || quantity == "" || isNaN(quantity)) {
        alert("Quantity must be only numbers");
        return false;
    }
   else{
    confirm("Confirm Order Submission ?");

}

  }
}());

</script>

这是一个表单验证脚本,对强制输入字段使用“必需”ID,运行良好。我只想在最后一个 else if 语句之后出现一个确认警报框,我尝试使用 confirm() 但即使我点击“取消”,表单仍然被提交

【问题讨论】:

    标签: javascript php jquery html forms


    【解决方案1】:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to display a confirm box.</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <script>
    var x;
    
    function myFunction() {
      x =  confirm("Press a button!");
      alert(x)
      if(x === true){
    
       alert("Call Submit !!!!");
    
      }else{
    
       alert("Don't Call Submit !!!!");
      }
    
    }
    
    
    </script>
    
    </body>
    </html>

    试试这个来自 w3school 的代码 显示一个确认框

    https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_confirm

    如果用户单击“确定”,confirm() 方法返回 true,否则返回 false。

    【讨论】:

    • 试过这个,点击取消,表单仍然提交......@Nisal Edu
    • 变量x获取confirm()返回的值(真/假)请在调用提交函数之前检查该值
    【解决方案2】:

    参考https://www.w3schools.com/jsref/met_win_confirm.asp可以使用confirm()方法。

    var txt;
    var r = confirm("Press a button!");
    if (r == true) {
        txt = "You pressed OK!";
        // Continue submit the form
    } else {
        txt = "You pressed Cancel!";
        // Do nothing or return to form
    } 
    

    【讨论】:

      【解决方案3】:
      else{
          if (confirm("Confirm Order Submission ?")) return true;
          else return false;       
          }
      

      这让它工作了!

      【讨论】:

        猜你喜欢
        • 2020-09-06
        • 1970-01-01
        • 2018-02-11
        • 1970-01-01
        • 2014-04-13
        • 2014-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多