将焦点设置到 jQuery 中的下一个输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1232379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the focus to the next input in jQuery?
提问by Nick
I've currently got a script that'll check for the value of a select box, and then enable a text field right next to it, and then hopefully set focus.
我目前有一个脚本,它将检查选择框的值,然后启用它旁边的文本字段,然后希望设置焦点。
I have this at the moment which works fine at enabling the input field...
我现在有这个可以很好地启用输入字段......
$("#assessed select").change(function () {
if($(this).val() == 'null') { $(this).next('input').attr("disabled", true); }
else { $(this).next('input').removeAttr("disabled"); }
});
I'm a bit stuck on the 'focus' bit to be honest, I tried "$(this).next('input').focus();" but that didn't focus at all, although it didn't bring up a Javascript error either...
老实说,我有点卡在“焦点”上,我试过“$(this).next('input').focus();” 但这根本没有重点,尽管它也没有出现 Javascript 错误......
$("#assessed select").change(function () {
if($(this).val() == 'null') { $(this).next('input').attr("disabled", true); $(this).next('input').focus(); }
else { $(this).next('input').removeAttr("disabled"); }
});
Any ideas please guys? I'm really stuck on this and it'd be a very simple, but very useful addition to the page I'm building!
大家有什么想法吗?我真的坚持这一点,这将是我正在构建的页面的一个非常简单但非常有用的补充!
Thanks
谢谢
采纳答案by redsquare
Modified of the above answer with cached this jq object. Also the filter inside next is not needed. next() will only ever return the next sibling. The filter is basically saying get me the next only if it is an input or whatever filter you give. If you are sure the next is the desired object there is no need to include the filter.
使用缓存的这个 jq 对象修改了上述答案。也不需要 next 里面的过滤器。next() 只会返回下一个兄弟。过滤器基本上是说只有当它是输入或您提供的任何过滤器时才让我下一个。如果您确定下一个是所需的对象,则无需包含过滤器。
$("#assessed select").change(function () {
var $this = $(this);
if( $this.val() === 'null') {
$this.next()
.attr("disabled", true);
}
else {
$this.next()
.removeAttr("disabled")
.focus();
}
});
回答by TimoSolo
Also found a nice little plugin to get the next input: http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/
还找到了一个不错的小插件来获取下一个输入:http: //jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/
$.fn.focusNextInputField = function() {
return this.each(function() {
var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select').not(':hidden');
var index = fields.index( this );
if ( index > -1 && ( index + 1 ) < fields.length ) {
fields.eq( index + 1 ).focus();
}
else {fields.first().focus();}
return false;
});
};
回答by Rob Knight
I think that your problem might be that it's not possible to set the focus to a disabled input control (at least in some browsers/operating systems).
我认为您的问题可能是无法将焦点设置为禁用的输入控件(至少在某些浏览器/操作系统中)。
Your focus()
call is basically in the wrong block - it should be in the else
block, not the if
block.
您的focus()
调用基本上是在错误的块中 - 它应该在else
块中,而不是if
块中。
Like this:
像这样:
$("#assessed select").change(function () {
if($(this).val() == 'null') { $(this).next('input').attr("disabled", true); }
else { $(this).next('input').removeAttr("disabled"); $(this).next('input').focus(); }
});