我有五个输入字段是否有可能避免在此情况下
<input data-category="0" class="ncr_input" type="text" name="name" maxlength="25" id="t1cat" value="" placeholder="T1" autocomplete="off"/>
<input data-category="1" class="ncr_input" type="text" maxlength="25" name="name" id="t2cat" value="" placeholder="T2" autocomplete="off" />
<input data-category="2" class="ncr_input" type="text" maxlength="25" name="name" id="t3cat" value="" placeholder="T4" autocomplete="off" />
<input data-category="3" class="ncr_input" type="text" maxlength="25" name="name" id="t4cat" value="" placeholder="T4" autocomplete="off" />
<input data-category="4" class="ncr_input" type="text" maxlength="25" name="name" id="t5cat" value="" placeholder="T4" autocomplete="off" />
我一定要限制在这五个文本字段中的某些共同特征的重复代码。
的一致风格onkeypress事件没有在移动工作(但是在桌面浏览器工作正常)
所以我做它这样
$(function() {
$('#t1cat').keypress(function(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode ==37 || charCode ==38 ) {
return false;
}
return true;
});
$('#t2cat').keypress(function(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode ==37 || charCode ==38 ) {
return false;
}
return true;
});
similar code for t3cat t4cat and t5cat also
});
这是工作正常,但我的问题是,有可能避免的重复代码