将焦点设置为jQuery中的下一个输入? [英] Setting the focus to the next input in jQuery?

查看:132
本文介绍了将焦点设置为jQuery中的下一个输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经有了一个脚本来检查选择框的值,然后在它旁边启用一个文本框,然后有望设置焦点。



我现在可以正常使用输入字段...

  $(#评估过($(this).val()=='null'){$(this).next('input')。attr( ());} 
else {$(this).next('input')。removeAttr(disabled);}

});

我有点卡在'重点'位,说实话,我试过$此)的.next( '输入')聚焦();。但它并没有集中在所有的,虽然它没有带来一个Javascript错误...

$ $ p $ code $( ($(this).val()=='null'){$(this).next('input')。attr (disabled,true); $(this).next('input')。focus();}
else {$(this).next('input')。removeAttr(disabled); }

});

有什么想法,请大家?我真的坚持这一点,这将是一个非常简单的,但非常有用的除了我建立的页面!



谢谢

解决方案

修改了上面的答案,缓存了这个jq对象。而且里面的过滤器也是不需要的。 next()只会返回下一个兄弟。该过滤器基本上是说,只有当它是一个输入或任何你给的过滤器,让我下一个。如果您确定下一个是所需的对象,则不需要包含过滤器。

  $(#评估选择 ).change(function(){
var $ this = $(this);
if($ this.val()==='null'){
$ this.next )
.attr(disabled,true);
}
else {
$ this.next()
.removeAttr(disabled)
.focus();
}
});


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...

$("#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

解决方案

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.

$("#assessed select").change(function () {
    var $this = $(this);
    if( $this.val() === 'null') {
        $this.next()
             .attr("disabled", true);
    }
    else {
        $this.next()
             .removeAttr("disabled")
             .focus();
    }
});

这篇关于将焦点设置为jQuery中的下一个输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆