最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - sweet alert immidiately closed before click OK button or hit enter when sweet alert appeared - Stack Overflow

programmeradmin6浏览0评论

im new using sweet alert, first i will show the code :

    $('#byr').keydown(function (e){
      var bayar = Number($('#byr').val());
      var total = Number($('.rp-harga').text());
      var kmb = $('.rp-kmb').text();

      if(e.keyCode == 13)
      {
        if(bayar<total)
        {
          alert("Uang Anda Kurang");
        }
        else {
          swal("Transaksi Berhasil", "Kembalian : Rp " + (bayar-total).toString(), "success");
        }
      }
    });

The problem is when i hit enter after fill the input text with else condition, it will show the sweet alert, but the sweet alert will immidiately closed before i click OK button or hit enter when sweet alert appear

I tried adding e.preventDevaults(), and get these problem :

  1. if i add it before swal code, the alert cant appear in else contidion
  2. if i add it after swal code, the same problem will happen (swal closed immidiately before click OK / hit enter).

Here is a working JSFiddle with my problem.

im new using sweet alert, first i will show the code :

    $('#byr').keydown(function (e){
      var bayar = Number($('#byr').val());
      var total = Number($('.rp-harga').text());
      var kmb = $('.rp-kmb').text();

      if(e.keyCode == 13)
      {
        if(bayar<total)
        {
          alert("Uang Anda Kurang");
        }
        else {
          swal("Transaksi Berhasil", "Kembalian : Rp " + (bayar-total).toString(), "success");
        }
      }
    });

The problem is when i hit enter after fill the input text with else condition, it will show the sweet alert, but the sweet alert will immidiately closed before i click OK button or hit enter when sweet alert appear

I tried adding e.preventDevaults(), and get these problem :

  1. if i add it before swal code, the alert cant appear in else contidion
  2. if i add it after swal code, the same problem will happen (swal closed immidiately before click OK / hit enter).

Here is a working JSFiddle with my problem.

Share Improve this question edited May 22, 2017 at 20:06 Lincoln Bergeson 3,4616 gold badges39 silver badges59 bronze badges asked May 22, 2017 at 17:19 janotamajanotama 2078 silver badges19 bronze badges 7
  • Can you provide your HTML as well? – Lincoln Bergeson Commented May 22, 2017 at 17:33
  • 1 Relevant question: stackoverflow./questions/29348785/… – Lincoln Bergeson Commented May 22, 2017 at 17:37
  • the HTML code just like this <input type="text" class="form-control input-sm" id="byr" >. I already see that post, but the question using button, and i have no idea on my work which it using input text – janotama Commented May 22, 2017 at 17:45
  • Try adding e.preventDefault(); when you call swal(). – Barmar Commented May 22, 2017 at 17:54
  • It's probably similar to stackoverflow./questions/44010744/… – Barmar Commented May 22, 2017 at 17:55
 |  Show 2 more ments

1 Answer 1

Reset to default 6

I assume you are using sweetalert2. This plugin has a configuration option:

allowEnterKey: If set to false, the user can't confirm the modal by pressing the Enter or Space keys, unless they manually focus the confirm button.

Therefore you can:

  • disable this feauture setting allowEnterKey to false
  • call swal after 10 milliseconds in order to overe the following src code for the first keydown:

    window.onkeydown = handleKeyDown;

$('#byr').keydown(function (e) {
    var bayar = Number($('#byr').val());
    var total = 0;

    if (e.keyCode == 13) {
        if (bayar < total) {
            alert("Uang Anda Kurang");
        }
        else {
            setTimeout(function() {
                swal({
                    title: "Transaksi Berhasil",
                    text: "Kembalian : Rp " + (bayar - total).toString(),
                    type: "success",
                    allowEnterKey: true // default value
                });
            }, 10)
        }
    }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr/sweetalert2/6.6.2/sweetalert2.min.css">
<script src="https://cdn.jsdelivr/sweetalert2/6.6.2/sweetalert2.min.js"></script>

<input type="text" id="byr">

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论