gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:29:29 25 4
gpt4 key购买 nike

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

关于javascript - 使用 jquery 验证一个组合输入值不等于另一组合输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24007969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com