如果我有50对输入文本框,即
<input type="text" id="name_0" /><input type="text" id="name_1" />
<input type="text" id="dept_0" /><input type="text" id="dept_1" />
...
<input type="text" id="age_0" /><input type="text" id="age_1" />
<input type="text" id="weight_0" /><input type="text" id="weight_1" />
即这些变量的50个。
当页面加载时,我用相同的数据填充每一对。
检查_0
是否与_1
不同的最佳方式是什么?
然后返回一条消息,显示哪一对已经改变。
比较应在更改值并单击按钮后进行。
答案 0 :(得分:2)
$("input[type=text]").each(function () {
var $this = $(this);
if ( /_0$/.test(this.id) ) {
if ( $this.val() != $this.next("input").val() ) {
$this.css("color", "red"); // or whatever
}
}
});
答案 1 :(得分:0)
Tomalak的答案应该有效,但是为了防止你的input
分散或者不一定在彼此旁边,这样的事情就足够了。
$('input:text[id$="_0"]').each(function() {
var new_id = this.id.replace('_0','_1');
if ($(this).val() !== $('input#'+new_id).val()) {
// not the same
}
});
答案 2 :(得分:0)
var changed = [];
$("[id$=_0]").each(function() {
var name = this.id.replace("_0", "");
if (this.value != $("#" + name + "_1").val()) {
changed.push(name);
}
});
console.log(changed);