2016-11-10 7 views
1

例6の入力(I can create more than 100)を検証しようとしていますが、これらの入力は動的に作成されています。ここでの問題はたとえばthe first two input values must be between 0 and 10, then other two between 5 and 10 and the last two inputs between 3 and 5です。私は何をしてIFでこれを検証しているが、私は、私は多くのコードを意味している100の以上の入力を作成することができます言ったように、ここで私が持っているものです。異なる値で複数の入力を検証する方法

$(document).ready(function() { 
 
    $('#create').change(function() { 
 
    draw(); 
 
    }); 
 
    
 
    $('#btn').click(function() { 
 
    validate(); 
 
    }); 
 
}); 
 

 
function draw() { 
 
    var total = $('#create').val(); 
 
    var html = ""; 
 
    for (var x = 0; x < total; x++) { 
 
    html += '<input id="InputNum' + x + '" />'; 
 
    } 
 
    $('#draw').html(html); 
 
} 
 

 
function validate() { 
 
    var Input1 = $('#InputNum0').val(); 
 
    var Input2 = $('#InputNum1').val(); 
 
    var Input3 = $('#InputNum2').val(); 
 
    var Input4 = $('#InputNum3').val(); 
 
    
 
    if (Input1 < 5 && Input1 >0 && Input2 < 5 && Input2 > 0) 
 
    alert("Hello"); 
 
    else 
 
    alert("Value must be between 0-5"); 
 
    
 
    if (Input3 < 15 && Input3 > 5 && Input4 < 15 && Input4 > 5) 
 
    alert("Hello"); 
 
    else 
 
    alert("Value must be between 5-15"); 
 
    
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<select id="create"> 
 
    <option selected disabled>Choose</option> 
 
    <option>6</option> 
 
<select /> 
 
    
 
    <div id="draw"> 
 
    </div> 
 
    
 
    <button id="btn">Valida</button>

私がどうかを知りたいです確認するすべての入力に対してifを使用する代わりに、これを行う簡単な方法がありますか?

+0

合計100個の入力があっても、最初の2入力を「0-5」、最後の2入力を「3-5」、残りの入力を「5-10」で検証しますか? – nstungcom

+0

@nstungcomはい!それはまさに私がやりたいことです –

答えて

0

eq()関数を使用します。

$("input").eq(-2) // before last element 
$("input").eq(-1) // last element 
関連する問題