[英]Set focus on input field for next element of certain class name
我正在尝试着重于元素的第一个输入字段,该元素的下一个是页面上的.search-options类。
这是我正在使用,但是它不起作用。
$(document).ready(function() {
$('.toggle-search-options').on('click', function() {
$(this).next('.search-options *:input[type!=hidden]:first').focus();
});
});
这非常类似于此: jQuery:查找不是同级的下一个元素 ,除了当前元素与您要查找的选择器不匹配。 在这种情况下,您只需要将其添加到选择中:
// Outside the event handler:
var $elements = $('.search-options *:input[type!=hidden]');
// Inside the event handler:
var $elementsWithCurrentElement = $elements.add(this);
$elementsWithCurrentElement.eq($elementsWithCurrentElement.index(this) + 1).focus();
如果您要查找的元素实际上是同级元素,请查看高效,简洁的方法来查找下一个匹配的同级元素? 。
我不明白在“输入”前面需要冒号。 这对我来说很好:
$('#test input[type!="hidden"]:first')
但是,作为一种替代解决方案,为什么不抢占匹配数组中的第一个呢?
var inputs = $('#test input[type!="hidden"]');
if(inputs.length > 0) { $(inputs[0]).focus(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.