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>
我想知道存在一個簡單的方法來做到這一點,而不是使用如果我想檢查每個輸入?
即使總共有100個輸入,您是否想用'0-5'確認前2個輸入,用'3-5'確認後2個輸入,其餘輸入是否爲'5-10'? – nstungcom
@nstungcom是的!這正是我想要做的 –