JQuery 查找焦点输入的索引 [英] JQuery find index of focused input

查看:27
本文介绍了JQuery 查找焦点输入的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到焦点输入字段的索引?现在我有这个代码:

How can I find index of a focused input field? For now I have this code:

var forma = $('form#mali_oglas'),
    pomoc = $('div[role=pomoc]'),    
    input = forma.find('input[type!=hidden], textarea');

    index = input.focus().index();
    console.log(index);

我得到的只是表单中输入元素的数量(目前为 15).

All that I get is number of input elements in the form (15 at the moment).

推荐答案

http://jsfiddle.net/iambriansreed/QyUHv/

jQuery

var f = $('form');
f.click(function(){    
    $('#index').val($(':input:focus',f).index());    
});​

HTML

<form>    
<input value="hello"/><br/>
<textarea></textarea><br/>
<input/><br/>
<input/><br/>
<select><option>Option</option></select><br/>
<input/><br/>
</form><br/><br/>
Field Index with focus:
<input id="index"/>

这篇关于JQuery 查找焦点输入的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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