【问题标题】:On submit form show modal and then press submit form在提交表单上显示模式,然后按提交表单
【发布时间】:2019-11-04 21:52:19
【问题描述】:

我已将此example 编码。

$(document).ready(function() {
  $('form').on('submit', function(e) {
    e.preventDefault();
    var quantityUser = $('#quantityUser').val(); // Read the user input
    var quantityW9 = $('#quantityW9').val(); //the number to compare

    if (quantityUser <= quantityW9) {
      this.submit();
    } else {
      $('#alertQuantity').modal('show');
    }
  });

  $('#cfmContinue').click("click", function(e) {
    console.log("submitttt");
    $('#myform').submit();
  });
});
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}
<script type="text/javascript" src="//code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link rel="stylesheet" type="text/css" href="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script type="text/javascript" src="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="container">
  <div class="row">
    <form id="myform" method="post" action="https://stackoverflow.com/" role="form">
      <div class="col-lg-3 my-2">
        <label for="firstName">Quantity</label>
        <input id="quantityUser" name="quantity" type="text" value="150" class="form-control" />
        <small id="quantityW9" class="form-text text-muted">100</small>
      </div>
      <div class="col-1 my-2">
        <button type="submit" class="btn btn-primary" autofocus>Go</button>
      </div>
    </form>
  </div>
  <div class="modal fade" id="alertQuantity" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLongTitle">Warning!</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <p>Are you sure;</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          <button id="cfmContinue" type="button" class="btn btn-primary">Submit</button>
        </div>
      </div>
    </div>
  </div>
</div>

在提交表单时,我进行了检查。如果此检查为真,我会向用户显示bootstrap modal 并带有一条消息。然后,用户可以继续提交表单,在模态中按下Submit 按钮。

但是,$('#myform').submit(); 不起作用并且表单未提交。

【问题讨论】:

  • 而不是this.submit,比如return true;
  • @KalpashreeV.Bal 因为他防止默认而无法工作
  • @mplungjan 感谢您的评论。我在 jsfiddle 链接中有所有代码,我在问题jsfiddle.net/aewh7pbf/4 中发布了这些代码
  • @mplungjan 你是对的。很抱歉。

标签: javascript jquery bootstrap-modal form-submit


【解决方案1】:
  • 获取比较值的.text()
  • 转换为数字
  • 调用 DOM 提交事件不再执行事件处理程序

$('#myform').on('submit', function(e) {
  var quantityUser = $('#quantityUser').val(); // Read the user input
  var quantityW9 = $('#quantityW9').text();    //the number to compare
  if (+quantityUser > +quantityW9) {
    e.preventDefault();
    $('#alertQuantity').modal('show');
  }
});

$('#cfmContinue').click("click", function(e) {
  console.log("submitttt");
  $('#myform')[0].submit();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="container">
  <div class="row">
    <form id="myform" method="post" action="https://stackoverflow.com/" role="form">

      <div class="col-lg-3 my-2">
        <label for="firstName">Quantity</label>
        <input id="quantityUser" name="quantity" type="text" value="150" class="form-control" />
        <small id="quantityW9" class="form-text text-muted">100</small>
      </div>

      <div class="col-1 my-2">
        <button type="submit" class="btn btn-primary" autofocus>Go</button>
      </div>
    </form>

  </div>


  <div class="modal fade" id="alertQuantity" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLongTitle">Warning!</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <p>Are you sure;</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          <button id="cfmContinue" type="button" class="btn btn-primary">Submit</button>
        </div>
      </div>
    </div>
  </div>

</div>

【讨论】:

  • @yaylitzis 不,它没有。你需要得到quantityW9的.text() - 我更新了
  • 转换为数字是通过在变量开头添加+来完成的?
  • 是的。我们需要获取没有 .val() 的 small 元素的 .text
  • 太棒了!再次非常感谢您提供的信息!
  • 为什么有效 $('#myform')[0].submit();而不是 $('#myform').submit(); .请指教
【解决方案2】:

@yaylitzis 实际上,您的代码“考虑到每次提交表单时都会触发 onSubmit”的问题是永远不会满足条件,因为即使您从模态 coz 数量确认用户数量大于 w9 数量。

要完成这项工作,您需要一个检查标志,以便您可以在您的条件下使用它,对于 quantityW9 也需要使用 .text() 而不是 val() 作为其非输入标签。

$(document).ready(function () {
  var checkPass = false;

  $('form').on('submit', function (e) {
    e.preventDefault();
    console.log("submit form");
    console.log("checkPass: ", checkPass);
    var quantityUser = parseInt($('#quantityUser').val());
    var quantityW9 = parseInt($('#quantityW9').text());
    console.log(quantityUser, quantityW9);

    if (quantityUser <= quantityW9 || checkPass == true) {
     console.log("Success");
     return true;
    } else {
     checkPass = false;
     console.log("Confirmation check");
     $('#alertQuantity').modal('show');
    }
 });

 $('#cfmContinue').click("click", function (e) {
   checkPass = true;
   $('body #myform').submit();
   $('#alertQuantity').modal('hide');
 });         
});

【讨论】:

  • 不。请参阅我的解决方案。只需使用 DOM 提交
  • @mplungjan 已经投票了,但是我在使用 DOM 提交时遇到了一些意外行为,这就是为什么我更喜欢以这种方式调用它。
猜你喜欢
  • 2018-03-25
  • 2014-05-15
  • 2021-07-20
  • 2014-01-19
  • 2020-02-18
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多