我想检查输入是否具有相同的值
输入是由循环生成的,每次数字都会不同
for(int i = 0; i< add; i++)
{%>
<input name="<%= i%>" id="<%= i%>" class="form-control" holder="S/Num"/>
<%} %>
我尝试过这个,但没有成功
<script>
$('input').blur(function() {
if ($('#s1').attr('value') == $('#s2').attr('value')) {
alert('Same Value'); return false;
} else { return true; }
});
</script>
最佳答案
试试这个:
$(function() {
var nums = $('input.form-control[id^=s]'); /* find all inputs starting id with `s` */
nums.on('change', function(e) {
var map = {};
nums.each(function(i, el) {
if (el.value.replace(/\s/g, '')) { /* trim whitespace */
if (undefined !== map[el.value]) {
console.log('duplicate value', el.value, ', found input with id', el.id);
el.value = ''; /* reset, if required */
} else {
map[el.value] = 1;
}
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="s1" id="s1" class="form-control" placeholder="S/Num" />
<input name="s2" id="s2" class="form-control" placeholder="S/Num" />
<input name="s3" id="s3" class="form-control" placeholder="S/Num" />
关于javascript - 检查具有相同值的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50574694/