CSh*_*ped 1 html javascript model-view-controller jquery autofocus
我正在尝试在第一个空表单字段上实现 AutoFocus 功能,该字段可以是输入元素或任何类型,即
1. textbox
2. radio button
3. Select
4. checkbox
Run Code Online (Sandbox Code Playgroud)
我查看了 jquery 网站并尝试使用:input选择器,但我无法在文本框以外的任何输入字段上实现自动对焦。我还设置了一个 JS Fiddle,
有人可以帮忙吗?
您可能需要使用filter()来查找具有空值的输入:
$(function () {
$("input:enabled").filter(function () {
return this.value.trim() == "";
}).first().focus();
});
Run Code Online (Sandbox Code Playgroud)
如果您使用的是checkbox. 所以为此:
$(function () {
$("input:enabled").filter(function () {
if ($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio")
return $(this).val().trim() == "" || !this.checked;
else
return this.value.trim() == "";
}).first().focus();
});
Run Code Online (Sandbox Code Playgroud)
小提琴:http : //jsfiddle.net/7fwrd2rk/