javascript - How to close the Bootstrap modal after submit? - Stack Overflow

Here I have a Bootstrap modal. My requirement is when I successfully submit the form with submit button

Here I have a Bootstrap modal. My requirement is when I successfully submit the form with submit button then I want to close the modal after some seconds. The problem here is when I enter some text instead of integer in my input or if I enter some invalid inputs and then when I click the submit button the input field shows the error and the modal closes after some seconds immediately.

I don't want to close the Bootstrap modal if the input field is invalid when clicking submit button.

How can I do it ?

EDIT: It works perfect with valid input.

html

 <div class="modal-body">
 <form action="">
     <input type="number" name="rows" min="0" value="0" max="10" required><br>
     <button type="submit" id="my_button" class="btn btn-info btn-sm">Submit</button>
 </form>
 </div>

script

<script>
 $('#my_button').click(function() {
    setTimeout(function() {$('#myModal').modal('hide');}, 4000);
});
</script>

Here I have a Bootstrap modal. My requirement is when I successfully submit the form with submit button then I want to close the modal after some seconds. The problem here is when I enter some text instead of integer in my input or if I enter some invalid inputs and then when I click the submit button the input field shows the error and the modal closes after some seconds immediately.

I don't want to close the Bootstrap modal if the input field is invalid when clicking submit button.

How can I do it ?

EDIT: It works perfect with valid input.

html

 <div class="modal-body">
 <form action="">
     <input type="number" name="rows" min="0" value="0" max="10" required><br>
     <button type="submit" id="my_button" class="btn btn-info btn-sm">Submit</button>
 </form>
 </div>

script

<script>
 $('#my_button').click(function() {
    setTimeout(function() {$('#myModal').modal('hide');}, 4000);
});
</script>
Share Improve this question edited Aug 6, 2020 at 5:26 kmoser 9,3183 gold badges26 silver badges44 bronze badges asked Aug 6, 2020 at 5:08 D_PD_P 86211 silver badges37 bronze badges 2
  • What happen when you enter valid input. does popup close after 4 seconds? – Vaibhav Commented Aug 6, 2020 at 5:15
  • @Vaibhav yes it works with valid input – D_P Commented Aug 6, 2020 at 5:17
Add a ment  | 

2 Answers 2

Reset to default 5

Don't set the timeout if the form has invalid values:

$('#my_button').click(function() {
  if ( ! $('form input:invalid' ).length ) {
    setTimeout(function() {$('#myModal').modal('hide');}, 4000);
  }
});

please try this code snippet

<div class="modal-body">
    <form action="">
        <input type="number" name="rows" min="0" value="0" max="10" required><br>
        <button type="submit" id="my_button" class="btn btn-info btn-sm">Submit</button>
    </form>
</div>

<script>
 $('#my_button').click(function() {
    setTimeout(function() {$('#myModal').modal('toggle');}, 4000);
});
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745076435a4609863.html

相关推荐

  • javascript - How to close the Bootstrap modal after submit? - Stack Overflow

    Here I have a Bootstrap modal. My requirement is when I successfully submit the form with submit button

    16小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信