使用jquery验证一个组合输入值不等于另一个组合输入

时间:2014-06-03 05:48:56

标签: javascript jquery html

我的表有问题,一行有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

我在数组中使用name,因为我有一个函数来添加另一个具有相同格式的新行,我需要使用POST方法传递每个值。

请帮帮我。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

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