专注于第一个表单元素,无论它是什么.j查询对话框
Focus on first form element, whatever it may be. jQuery dialog
>我有一个jQuery模式对话框,当它打开时,我希望它专注于第一个表单元素。
目前我有这个:
the_dialog.dialog({
modal: true,
width: 700,
title: 'title',
close: suicide ,
open: function(event, ui) {
setTimeout(function() {
jQuery('#').focus(); <-- VERY SPECIFIC CSS SELECTOR PERHAPS?
}, 220);
}
}
);
我的问题是这个对话框是从我的应用程序中的几个不同位置调用的,第一个表单元素有时可以是input
,有时可以是select
。
的布局始终相同,只有第一个表单元素可能会发生变化。
<table>
<tbody>
<tr>
<td>LABEL</td>
<td>FIRST FORM ELEMENT</td>
</tr>
<tr>
<td>LABEL</td>
<td></td>
</tr>
<tr>
<td>LABEL</td>
<td></td>
</tr>
<tr>
<td>LABEL</td>
<td></td>
</tr>
</tbody>
</table>
在不添加任何类或 ID 的情况下,当表单打开时,我可以专注于第一个表单元素,无论它是什么?
可以在打开的回调中使用伪:input
选择器并查找第一个非隐藏元素
open: function(event, ui) {
the_dialog.find(':input:not(:hidden):first').focus()
}
:input
过滤标签<input>
、<textarea>
和<select>
:hidden
过滤任何在display:none
和type="hidden"
中不可见的标签
你可以
试试
jQuery('input,select').first().focus();
关于注释,您应该将其范围限定为仅适用于对话框,例如
http://jsfiddle.net/chkfgfwy/
您可以使用:input
选择器在对话框中查找第一个表单元素:
the_dialog.find(':input:first').focus()
我建议:
// I'm assuming that <textarea> elements may be used, they can be removed if not;
// 'dialog' is an assumed reference to a jQuery object containing the relevant <table>:
dialog.find('input, select, textarea')
// retrieving the first element matched by the selector:
.eq(0)
// focusing that found element:
.focus();
// this part is entirely irrelevant, and used only to vary the "first form element",
// in order to demonstrate the approach working, regardless of which element is 'first':
var formElements = ['<input />', '<select></select>', '<textarea></textarea>'];
$('td:nth-child(2)').html(function(i) {
return $(formElements[Math.floor(Math.random() * formElements.length)]).val(i);
});
// the relevant part (explained above):
$('input, select, textarea').eq(0).focus();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>LABEL</td>
<td></td>
</tr>
<tr>
<td>LABEL</td>
<td></td>
</tr>
<tr>
<td>LABEL</td>
<td></td>
</tr>
<tr>
<td>LABEL</td>
<td></td>
</tr>
</tbody>
</table>
引用:
- JavaScript:
-
Math.random()
. -
Math.floor
.
-
- j查询:
-
eq()
. -
focus()
. -
html()
.
-
我们可以<th>
标签来包装您的标签吗?
然后你可以使用
$('table').find('td').eq(0).children().eq(0).focus()
虽然它有点冗长,但如果孩子是输入、选择或其他任何东西(文本区域等)会起作用
相关文章:
- 使用Angular存储用户以前是否选中过复选框,然后再调用它的最佳方式是什么
- 什么是淘汰赛中的根管理员.js以及它指向什么
- 使用查询将对象数组发布到asp.net-mvc控制器操作的正确方法是什么
- 在 http 请求中添加事件侦听器 ( request.on ) 是什么意思?而笏就是它的用途
- _.indexOf() 'item !== item' - 它的目的是什么
- jQuery"return{foo:bar,foo2:bar2}"-它是什么
- JavaScript Harmony-它是什么
- Reflect.js,它是什么
- 如何匹配其他一切,但不管它是什么's与regex匹配
- 专注于第一个表单元素,无论它是什么.j查询对话框
- 是否有可用于<对象>的Javascript“Evoke”函数 - 如果有,它是什么
- 它是什么类型的Javascript模式
- sequelize client.uuid,它是什么,它有什么效果
- window.location.hash-它是什么,以及它的用例
- Websocket客户端(浏览器)接收到一个零长度的消息-它是什么?
- 它是什么(function($) {})();的意思
- 它是什么<<我的意思是在Javascript中
- 如果我们只知道一个元素的ID,如何检查它是什么?
- 如何防止PHP中的echo,并得到它是什么
- 如何在JavaScript中查看对象以了解它是什么