我正在尝试使用jQuery框架,因为我正面临性能问题。
我总共有8个文本字段,其中4个是Type1
,其余4个是Type2,每个Type1 Type2
都有一对,这样它们就是彼此的替代品。现在我的目标是使用jQuery进行验证,例如如果选择了Type1
,Type2
的值将为空,反之亦然。
我的文字字段有名称:
Type1-1
Type1-2
Type2-1
Type2-2
Type3-1
Type3-2
Type4-1
Type4-2
我的代码:
for (var i = 1; i <= 4; i++) {
$("#Type1-" + i).on('click focusin', function () {
console.log("A",this.id);
$("#Type2-" + i).val('');
$( this).unbind( "focusin" );
});
$("#Type2-" + i).on('click focusin', function () {
console.log("B",this.id);
$("#Type1-" + i).val('');
$( this).unbind( "focusin" );
});
$("#Type1-" + i).keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
});
$("#Type2-" + i).keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
});
};
答案 0 :(得分:1)
如果我理解正确,当点击文本框时,你只想清除所有相同类型的文本框,对吗?
如果是这样的话,这样就足够了:
$(function(){
$('input').on('focus',function(){
$("." + this.className).val('');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input class="t1"/><br/>
<input class="t1"/><br/>
<br/>
<input class="t2"/><br/>
<input class="t2"/><br/>
<input class="t2"/><br/>
<br/>
<input class="t3"/><br/>
<input class="t3"/><br/>
<input class="t3"/><br/>
<input class="t3"/><br/>