jQuery 动态聚焦于第一个 INPUT 或 Textarea
这是一个棘手的早晨开始。
我有一系列的图标。当您单击图标时,它会加载一个表单。一些表单有输入[文本],其他表单有文本区域。
我想要提出的是 jQuery,一旦我加载了表单,我就可以运行它将...专注于第一个输入或文本区域,无论它是什么,都是动态的,所以我不需要 if 块。
有想法吗?
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.
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.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我认为这应该可以
This should do it I think
这里是:
#form-id是表单的ID;
:input 选择任何输入和文本区域元素;
:enabled 确保输入可编辑;
:visble 确保元素可见;
: 首先很明显
Here it is:
#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
你的一些代码会很好..但这应该很容易。
假设您将 for 加载到您知道 id 的 div 中...
您所要做的就是
加载表单之后
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....
all you have to do is something like
after you've loaded your form
:input
选择器抓取所有输入、文本区域、选择和按钮元素。The
:input
selector grabs all input, textarea, select and button elements.编辑
这实际上并不是一个很好的解决方案。以下 Patricia 和 Onkelborg 的解决方案要优雅得多。
EDIT
This is actually not that great of a solution. Patricia's and Onkelborg's solutions below are much more elegant.
它在加载事件中对我有用。
It's working for me in the load event.
这是我的解决方案。代码应该很容易理解,但想法如下:
代码:
然后只需调用focusFirst 与您的父元素或选择器。
选择器:
元素:
Here is my solution. The code should be easy enough to follow but here is the idea:
The code:
Then simply call focusFirst with your parent element or selector.
Selector:
Element:
这是 @craztmatt 和其他人在此基础上开发的引导模式解决方案。我已经扩展为从触发事件的模式开始选择目标元素。这个捕获输入、文本区域和选择元素。
不完全是OP的问题,但也应该适用于纯jquery情况。
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.
Not precisely the OP's question but should be adaptable to a pure jquery case too.
使用:
或:
使用
.focus()
时还要记住Use instead:
or:
Also bear in mind when using
.focus()
在寻找快速解决方案以选择具有空白值的第一个输入时发现了这个问题。
提出以下内容,希望对某人有所帮助
Found this question when looking for a quick solution to select the first input with a blank value.
Came up with the following, hope it helps someone