热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

jQuery动态关注第一个INPUT或Textarea-jQueryDynamicallyfocusonthefirstINPUTorTextarea

Heresatrickyonetostartthemorning.从早上开始,这是一个棘手的问题。Ihaveaseriesoficons.Whenyouclick

Here's a tricky one to start the morning.

从早上开始,这是一个棘手的问题。

I have a series of icons. When you click an icon it loads a form. Some of the forms have input[text] others have textareas.

我有一系列的图标。单击图标时,它会加载表单。有些表格有输入[text],其他表格有textareas。

What I'm trying to come up with is jQuery that once I load the form I can run that will... Focus in on the first input or textarea whatever it may be, dynamically so I don't need if blocks.

我想要提出的是jQuery,一旦我加载了表单,我就可以运行...将注意力集中在第一个输入或textarea,无论它是什么,动态,所以我不需要if块。

Ideas?

想法?

9 个解决方案

#1


59  

This should do it I think

我认为应该这样做

$("#formId input:text, #formId textarea").first().focus();

#2


39  

Here it is:

这里是:

$('#form-id :input:enabled:visible:first').focus();

#form-id is ID of the form; :input selects any input and textarea element; :enabled ensures the input is editable; :viisble ensures that the element is visible; :first is obvious

#form-id是表单的ID; :input选择任何input和textarea元素; :enabled确保输入可编辑; :viisble确保元素可见; :首先是显而易见的

#3


10  

$(":input:first").focus(); 

The :input selector grabs all input, textarea, select and button elements.

:输入选择器获取所有输入,textarea,select和button元素。

#4


9  

some of your code would be nice.. but this should be easy.

你的一些代码会很好..但这应该很容易。

assuming your loading your for into a div that you know the id of....

假设你加载你的div进入你知道的id ...

all you have to do is something like

所有你需要做的就是这样

$('#TheIdOfYourFormDiv').find('input, textarea').first().focus()

after you've loaded your form

在您加载表单后

#5


4  

EDIT

编辑

This is actually not that great of a solution. Patricia's and Onkelborg's solutions below are much more elegant.

这实际上并不是一个很好的解决方案。 Patricia和Onkelborg的解决方案更加优雅。

var $firstInput = jQuery("input:first");
var $firstTextArea = jQuery("textarea:first");

if(firstInput.length == 0) {
  $firstTextArea.focus();
}

else {
  $firstInput.focus();
}

#6


1  

It's working for me in the load event.

它在load事件中对我有用。

$('#Div').find('input:first').focus();

#7


1  

Here is my solution. The code should be easy enough to follow but here is the idea:

这是我的解决方案。代码应该很容易遵循,但这里的想法是:

  • get all inputs, selects, and textareas
  • 获取所有输入,选择和textareas
  • filter out all buttons and hidden fields
  • 过滤掉所有按钮和隐藏字段
  • filter to only enabled, visible fields
  • 过滤到仅启用的可见字段
  • select the first one
  • 选择第一个
  • focus the selected field
  • 聚焦所选字段

The code:

代码:

function focusFirst(parent) {
    $(parent).find('input, textarea, select')
        .not('input[type=hidden],input[type=button],input[type=submit],input[type=reset],input[type=image],button')
        .filter(':enabled:visible:first')
        .focus();
}

Then simply call focusFirst with your parent element or selector.

然后只需使用父元素或选择器调用focusFirst。

Selector:

选择:

focusFirst('#parentId');

Element:

元件:

var el = $('#parentId');
focusFirst(el);

#8


1  

Here's A solution for bootstrap modals developed on top of that by @craztmatt and others. I have extended to have the selection of the target element start at the modal that triggered the event. This one catches inputs, textarea AND select elements.

这是由@craztmatt和其他人开发的自助模态解决方案。我已经扩展到在触发事件的模态中选择目标元素。这个捕获输入,textarea和选择元素。

// do this to make bootstrap modals auto-focus.
// delegate to body so that we only have to set it up once.
$('body').on('shown.bs.modal', function (e) {
    var ele = $(e.target).find('input[type=text],textarea,select').filter(':visible:first'); // find the first input on the bs modal
    if (ele) {ele.focus();} // if we found one then set focus.
})

Not precisely the OP's question but should be adaptable to a pure jquery case too.

不完全是OP的问题,但也应该适应纯粹的jquery案例。

#9


0  

Because :visible is a jQuery extension and not part of the CSS specification, queries using :visible cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :visible to select elements, first select the elements using a pure CSS selector, then use .filter(":visible").

因为:visible是一个jQuery扩展而不是CSS规范的一部分,使用:visible的查询无法利用本机DOM querySelectorAll()方法提供的性能提升。要在使用时获得最佳性能:可见选择元素,首先使用纯CSS选择器选择元素,然后使用.filter(“:visible”)。

Use instead:

改为使用:

$('form').filter(':input:first').focus();

or:

要么:

$('input').first().focus(); //as shown in the "correct" answer.

Also bear in mind when using .focus()

使用.focus()时也要记住

Attempting to set focus to a hidden element causes an error in Internet Explorer. Take care to only use .focus() on elements that are visible. To run an element's focus event handlers without setting focus to the element, use .triggerHandler( "focus" ) instead of .focus().

尝试将焦点设置为隐藏元素会导致Internet Explorer出错。注意只对可见的元素使用.focus()。要在不将焦点设置到元素的情况下运行元素的焦点事件处理程序,请使用.triggerHandler(“focus”)而不是.focus()。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • 本文介绍了关于Java异常的八大常见问题,包括异常管理的最佳做法、在try块中定义的变量不能用于catch或finally的原因以及为什么Double.parseDouble(null)和Integer.parseInt(null)会抛出不同的异常。同时指出这些问题是由于不同的开发人员开发所导致的,不值得过多思考。 ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • 学习Java异常处理之throws之抛出并捕获异常(9)
    任务描述本关任务:在main方法之外创建任意一个方法接收给定的两个字符串,把第二个字符串的长度减1生成一个整数值,输出第一个字符串长度是 ... [详细]
author-avatar
伊金芳60442
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有