Moh*_*ain 2 jquery focus onkeyup
注意:我的页面只有文本框.没有其他的.(没有别的=>没有其他输入类型)
$(":input[type='text']").keyup(function(event){
if(valid)
{
// take a focus to next input element, which is again a text type.
}
});
Run Code Online (Sandbox Code Playgroud)
如何在键入后将焦点跳到下一个输入元素.
Sarfraz回答: -
<div>
<input type="text" maxlength="1" size="1" >
<input type="text" maxlength="1" size="1" >
<input type="text" maxlength="1" size="1" > // sarfraj -- here it crash Please check below for error.
</div>
<div>
<input type="text" maxlength="1" size="1" >
<input type="text" maxlength="1" size="1" >
<input type="text" maxlength="1" size="1" >
</div>
Run Code Online (Sandbox Code Playgroud)
从firebug问题是
$(this).next("[type=\"text\"]")[0] is undefined
[Break On This Error] $(this).next('[type="text"]')[0].focus();
Run Code Online (Sandbox Code Playgroud)
以下是修改代码的方法:
$(":input[type='text']").keyup(function(event){
if(valid) {
if ($(this).next('[type="text"]').length > 0){
$(this).next('[type="text"]')[0].focus();
}
else{
if ($(this).parent().next().find('[type="text"]').length > 0){
$(this).parent().next().find('[type="text"]')[0].focus();
}
else {
alert('no more text input found !');
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
如何在键入后将焦点跳到下一个输入元素.
使用next()有focus:
$(this).next('[type="text"]')[0].focus();
Run Code Online (Sandbox Code Playgroud)
所以这是你的代码应该是这样的:
$(":input[type='text']").keyup(function(event){
if(valid) {
$(this).next('[type="text"]')[0].focus();
}
});
Run Code Online (Sandbox Code Playgroud)