Javascript 比较50对输入文本框
如果我有50对输入文本框,即Javascript 比较50对输入文本框,javascript,jquery,Javascript,Jquery,如果我有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" /> i、 e这些变量的50个 当页面加载时,我用相同的数据填充每一对 检查\u 0与\u 1是否不同的最佳方法是什么 然后返回一条消息,显示哪对已
<input type="text" id="name_0" /><input type="text" id="name_1" />
<input type="text" id="dept_0" /><input type="text" id="dept_1" />
i、 e这些变量的50个
当页面加载时,我用相同的数据填充每一对
检查\u 0
与\u 1
是否不同的最佳方法是什么
然后返回一条消息,显示哪对已更改
一旦更改了值并单击了一个按钮,就应该进行比较。Tomalak的答案应该有效,但为了防止您的
输入分散或不一定彼此相邻,类似的东西应该足够了
$("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
}
}
});
$('input:text[id$="_0"]').each(function() {
var new_id = this.id.replace('_0','_1');
if ($(this).val() !== $('input#'+new_id).val()) {
// not the same
}
});
您希望何时比较这些值?在键入或其他事件时?在按钮上单击事件。
$('input:text[id$="_0"]').each(function() {
var new_id = this.id.replace('_0','_1');
if ($(this).val() !== $('input#'+new_id).val()) {
// not the same
}
});
var changed = [];
$("[id$=_0]").each(function() {
var name = this.id.replace("_0", "");
if (this.value != $("#" + name + "_1").val()) {
changed.push(name);
}
});
console.log(changed);