我有大约 100 个类名为 questionOrder
的输入字段。我必须检查用户是否在表单输入中给出了任何重复的条目。
目前正在尝试类似的事情:
$('.questionOrder').on('input', function(){
var inp = this.value;
$('.questionOrder').each(function(){
if(inp==this.value){
console.log('match');
}else{
console.log('not match');
}
})
})
在字段上输入时出现问题,它也会自行检查
最佳答案
尝试使用 not()
从选择器中删除 this
元素,如下所示:
$('.questionOrder').not(this).each(function(){
工作代码示例:
$('.questionOrder').on('input', function(){
var inp = this.value;
$('.questionOrder').not(this).each(function(){
if(inp==this.value){
console.log('match');
}else{
console.log('not match');
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="questionOrder" value="first"/>
<input type="text" class="questionOrder" value="second"/>
<input type="text" class="questionOrder" value="third"/>
<input type="text" class="questionOrder"/>
关于javascript - 如何使用 jQuery 检查是否给出了相同的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51571147/