簡體   English   中英

使用jQuery驗證一個組合輸入值不等於另一個組合輸入

[英]validate one combination input value not to be equal to another combination input with jquery

我的表在一行中有4個輸入存在問題:

<tr>
  <td><input type=text name='section[]' class='section'></td>
  <td><select name='finish[]' class='finish'>............</select></td>
  <td><select name='hrd[]' class='hrd'>............</select></td>
  <td><input type=text name='length[]' class='length'></td>
</tr>
<tr>
  <td><input type=text name='section[]' class='section'></td>
  <td><select name='finish[]' class='finish'>............</select></td>
  <td><select name='hrd[]' class='hrd'>............</select></td>
  <td><input type=text name='length[]' class='length'></td>
</tr>
<tr>
  <td><input type=text name='section[]' class='section'></td>
  <td><select name='finish[]' class='finish'>............</select></td>
  <td><select name='hrd[]' class='hrd'>............</select></td>
  <td><input type=text name='length[]' class='length'></td>
</tr>
...
...
...

我需要做的是使用jquery驗證每一行中的組合輸入不等於另一行。

//Wrong example:

row 1: section=>0001; finish=>A; hrd=>12; length=>2000
row 2: section=>0001; finish=>A; hrd=>12; length=>2000 //it is wrong because the combination already exist in row 1 or another row

//they have to be like this:

row 1: section=>0001; finish=>A; hrd=>11; length=>2000
row 2: section=>0001; finish=>A; hrd=>12; length=>2000 //different hrd
row 3: section=>0001; finish=>B; hrd=>12; length=>2000 //different finish
.... //at least there are one different input

我使用數組中的名稱,因為我有一個函數來添加具有相同格式的另一行,並且我需要使用POST方法傳遞每個值。

請幫我。 任何幫助將不勝感激。 謝謝。

var match = false;
var rows = $("tr");
for (var i = 0; !match && i < rows.length-1; i++) {
    var section = $(".section", rows[i]).val(),
        finish = $(".finish", rows[i]).val(),
        hrd = $(".hrd", rows[i]).val(),
        length = $(".length", rows[i]).val();
    for (var j = i+1; j < rows.length; j++) {
        if ($(".section", rows[j]).val() == section &&
            $(".finish", rows[j]).val() == finish &&
            $(".hrd", rows[j]).val() == hrd &&
            $(".length", rows[j]).val() == length) {
            match = true;
            break;
        }
    }
}
if (match) {
    alert ("Rows must all be different");
}

DEMO

暫無
暫無

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

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