2017-07-12 8 views
1

ユーザは、フォームの中で、整数を埋めます。彼がフォームを提出するとき、私はその番号をチェックしたい。 number < 100の場合は、フォームが正常に提出されます。そうでない場合は、確認ウィンドウを表示し、[OK]を選択するとフォームを送信します。入力を確認した後にフォームが提出されない

$('form').on('submit', function(e) { 
 
    e.preventDefault(); 
 
    var quantity = $('#quantity').val(); // Get the user input. 
 
    var quantityToCompare = 100; //the number to compare 
 

 
    if (quantity < quantityToCompare) { 
 
    console.log("Submit the form"); 
 
    $('form').submit(); 
 
    } else { 
 
    console.log(quantity + " is bigger from " + quantityToCompare); 
 

 
    var confirmation = confirm("Do you want to continue with the submit ?"); 
 
    if (confirmation) { 
 
     console.log("submitting now ..."); 
 
     $('form').submit(); 
 
    } else { 
 
     console.log("Clicked Cancel"); 
 
    } 
 
    } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 

 

 
<div class="row"> 
 
    <div class="col-md-12"> 
 
    <form id="form" method="post" role="form"> 
 

 
     <label class="col-md-1 control-label">Quantity</label> 
 
     <div class="col-md-2"> 
 
     <input id="quantity" type="number" class="form-control" name="quantity" required> 
 
     </div> 
 

 
     <div class="form-group"> 
 
     <div class="col-sm-2"> 
 
      <button type="submit" name="formAction" class="btn btn-success">Submit</button> 
 
     </div> 
 
     </div> 
 

 
    </form> 
 
    </div> 
 
</div>

私は2つの問題があります。ユーザーがnumber < 100を入力した場合、エラーtoo much recursionが表示され、ユーザーが大きな数字を入力してポップアップウィンドウが開いていてフォームを送信できない場合は、

私には何が欠けていますか?

+0

を提出することが防止される;)ちょうどコンソールを見て(F12) –

+0

なぜだろうあなたは '$( 'form')を持っています。submit();'フォームの中にsubmit? –

+0

私の考えは.. 1.提出を防ぎます。2.あなたが望む条件を確認します。3.数字が小さい場合は提出を続けます。 4.入力を確認するようにユーザーに要求しない場合。 5.彼がokをクリックすると、 – yaylitzis

答えて

2

からとき、再び、なぜその所与の警告.thatループ効果を生み出すとagain.Doあなたの機能削除jQuery submitイベントハンドラ内の条件に基づいてフォームを作成する場合は、ネイティブサブミットイベントを呼び出す必要があります。それ以外の場合は、イベントハンドラがもう一度呼び出され、フォームあなたは `submit`イベントを上書きし、stackoverflowのまで、無限ループが発生している

$('form').on('submit', function(e) { 
    e.preventDefault(); 
    var quantity = $('#quantity').val(); // Get the user input. 
    var quantityToCompare = 100; //the number to compare 

    if (quantity < quantityToCompare) { 
    this.submit(); 
    } else { 
    var confirmation = confirm("Do you want to continue with the submit ?"); 
    if (confirmation) { 
     this.submit(); 
    } else { 
     console.log("Clicked Cancel"); 
    } 
    } 
}); 
+0

ありがとうございました! – yaylitzis

1

e.preventDefault();は、送信ハンドラで送信できません。あなたが提出を許可したくないときにだけそれをしてください。

EDIT additonで

:あなたが再帰的に同じイベントを呼び出します$('form').submit()を呼ぶと再び阻止され、何度も何度も再帰的に呼び出します 場合でも。したがって、提出することに興味がある条件では、e.preventDefault();をそのブロックでのみ呼び出し、$('form').submit()を削除すると、提出されたため、すでに送信イベントになっています。

0

$( 'form')。submit(); このコードは再帰的に呼び出します。

+0

はい、送信し、preventDefaultが最初に防止するたびに続行します。 –

0

form submit機能.its内部form submitを送信するには正常に送信

$('form').on('submit', function(e) { 
 
    e.preventDefault(); 
 
    var quantity = $('#quantity').val(); // Get the user input. 
 
    var quantityToCompare = 100; //the number to compare 
 

 
    if (quantity < quantityToCompare) { 
 
    console.log("Submit the form"); 
 
    //do your function 
 
    } else { 
 
    console.log(quantity + " is bigger from " + quantityToCompare); 
 
    var confirmation = confirm("Do you want to continue with the submit ?"); 
 
    if (confirmation) { 
 
     console.log("submitting now ..."); 
 
     //do your function 
 
    } else { 
 
     console.log("Clicked Cancel"); 
 
    } 
 
    } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 

 

 
<div class="row"> 
 
    <div class="col-md-12"> 
 
    <form id="form" method="post" role="form"> 
 

 
     <label class="col-md-1 control-label">Quantity</label> 
 
     <div class="col-md-2"> 
 
     <input id="quantity" type="number" class="form-control" name="quantity" required> 
 
     </div> 
 

 
     <div class="form-group"> 
 
     <div class="col-sm-2"> 
 
      <button type="submit" name="formAction" class="btn btn-success">Submit</button> 
 
     </div> 
 
     </div> 
 

 
    </form> 
 
    </div> 
 
</div>

0

$('#submit').on('click', function(e) { 
 
    e.preventDefault(); 
 
    var quantity = $('#quantity').val(); // Get the user input. 
 
    var quantityToCompare = 100; //the number to compare 
 
    var submitFlag = true; 
 
    if (quantity < quantityToCompare) { 
 
    submitFlag = true; 
 
    } else { 
 
    submitFlag = false; 
 
    } 
 

 
    if (!submitFlag) { 
 
    var confirmation = confirm("Do you want to continue with the submit ?"); 
 
    if (confirmation) { 
 
     submitFlag = true; 
 
    } else { 
 
     console.log("Clicked Cancel"); 
 
    } 
 
    } 
 

 
    if (submitFlag) 
 
    console.log("SUBMIT"); // $("#form").submit(); 
 
    else 
 
    return false; 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 

 

 
<div class="row"> 
 
    <div class="col-md-12"> 
 
    <form id="form" method="post" role="form" action='http://www.google.com'> 
 

 
     <label class="col-md-1 control-label">Quantity</label> 
 
     <div class="col-md-2"> 
 
     <input id="quantity" type="number" class="form-control" name="quantity" required> 
 
     </div> 
 

 
     <div class="form-group"> 
 
     <div class="col-sm-2"> 
 
      <button name="formAction" id="submit" class="btn btn-success">Submit</button> 
 
     </div> 
 
     </div> 
 

 
    </form> 
 
    </div> 
 
</div>

関連する問題