簡體   English   中英

如何使用不同的值驗證多個輸入

[英]How to validate multiple inputs with different values

我試圖驗證例如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我想要檢查的每個輸入?

使用eq()函數。

$("input").eq(-2) // before last element
$("input").eq(-1) // last element

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM