function isExist(newEntry){
return Array.from($('tr[id*=output_newrow]'))
.some(element => $('td:nth(2)', $(element)).html() === newEntry );
}
newEntry
是要添加的输入文本的值 Then :
$('.add').click(function () {
textInput = "";
$('.TextInput').empty();
textInput = $(this).prev('.TextInput').val();
if(isExist(textInput)){
alert("you have entered that output number for that output type")
}else{
//.....insert code
}
})
演示:
http://jsfiddle.net/abdennour/MKfLU/27/
“但它适用于不同的选择选项,但也适用于相同的输入数字......我可以做 isExist (textInput)AND(type) 吗?” 如果你想在测试中嵌入类型:
function isExistx(newEntry,outtype){
return Array.from($('tr[id*=output_newrow]')).some( el =>
( $('td:nth(1)',$(el)).html() === outtype ) && ($('td:nth(2)',$(el)).html() === newEntry)
);
}
然后 :
if(isExistx(textInput,type)){
alert('you have entered that output number for that output type')
}else {
$('#status_table tr:last').after(str);
}
演示
http://jsfiddle.net/abdennour/MKfLU/29/