请帮助验证具有相同ID的文本框的重复值,该文本框与onkeyup或onblur事件一起循环,我需要在提交表单之前进行验证,
在循环中生成N个文本框,在每个文本框中我需要输入排名,因此我需要验证在将值输入文本框后不应输入重复排名,
enter code here
<%Iterator iterator_t = tem.keySet().iterator();
int i =0;
while (iter.hasNext()) {
String key_t = (String) iter.next();
ModuleBean moduleBean = (ModuleBean) tem.get(key_t); %>
<input type="text" name="rank1" id="rank1" size="2" "/>
<%}%>
答案 0 :(得分:2)
您不能为它使用相同的ID,而是为其添加一个类。 Demo
<input type="text" name="rank1" id="rank1" size="2" class="rank" value="1"/>
<input type="text" name="rank2" id="rank2" size="2" class="rank" value="2"/>
function find_duplicates(arr) {
var len=arr.length,
out=[],
counts={};
for (var i=0;i<len;i++) {
var item = arr[i];
var count = counts[item];
counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;
}
for (var item in counts) {
if(counts[item] > 1)
out.push(item);
}
return out;
}
$('.rank').keyup(function()
{
var listOfValues = [];
$('.rank').each(function()
{
if($(this).val()!='')
listOfValues.push($(this).val());
});
var duplicates = find_duplicates(listOfValues);
if(duplicates.length>0)
{
$('#result').html('Duplicates are:');
$('#result').append(JSON.stringify(duplicates));
}
else
{
$('#result').html('No Duplicates found');
}
});