Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 自动对焦窗体中的第一个空字段_Javascript_Jquery_Html_Model View Controller_Autofocus - Fatal编程技术网

Javascript 自动对焦窗体中的第一个空字段

Javascript 自动对焦窗体中的第一个空字段,javascript,jquery,html,model-view-controller,autofocus,Javascript,Jquery,Html,Model View Controller,Autofocus,我试图在第一个空表单字段上实现自动对焦功能,该字段可以是输入元素或任何类型,例如 1. textbox 2. radio button 3. Select 4. checkbox 我查看了jquery网站,并尝试使用:input选择器 但除了文本框之外,我无法在任何输入字段上实现自动对焦。 我还安装了JS小提琴 有人能帮忙吗?您可能需要使用查找空值的输入: $(function () { $("input:enabled").filter(function () {

我试图在第一个空表单字段上实现自动对焦功能,该字段可以是输入元素或任何类型,例如

1. textbox
2. radio button
3. Select
4. checkbox
我查看了jquery网站,并尝试使用:input选择器 但除了文本框之外,我无法在任何输入字段上实现自动对焦。 我还安装了JS小提琴

有人能帮忙吗?

您可能需要使用查找空值的输入:

$(function () {
    $("input:enabled").filter(function () {
        return this.value.trim() == "";
    }).first().focus();
});
如果您正在使用
复选框
,则此选项将不起作用。因此:

$(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();
});
Fiddle:

您可能需要使用来查找具有空值的输入:

$(function () {
    $("input:enabled").filter(function () {
        return this.value.trim() == "";
    }).first().focus();
});
如果您正在使用
复选框
,则此选项将不起作用。因此:

$(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();
});
Fiddle:

您需要清除空的/未检查的输入,并将焦点设置为匹配集中的第一个元素,如下所示

$(function () {
    $("input:enabled").filter(function () {
        return this.type.match(/checkbox|radio/i) && //when it's a checkbox/radio
               !this.checked || //get me a list of unselected ones
               ($.trim(this.value) === ""); // else get me a list of empty inputs
    }).first().focus(); //grab the first from the list and set focus
});
这是同样的道理

包含文本区域并选择。

您需要清除空/未选中的输入,并将焦点设置为匹配集中的第一个元素,如下所示

$(function () {
    $("input:enabled").filter(function () {
        return this.type.match(/checkbox|radio/i) && //when it's a checkbox/radio
               !this.checked || //get me a list of unselected ones
               ($.trim(this.value) === ""); // else get me a list of empty inputs
    }).first().focus(); //grab the first from the list and set focus
});
这是同样的道理


包括文本区域和选择。

你读过问题了吗,如果有意义的话?你读过问题了吗,如果有意义的话?嗨,Praveen,对我没用,看这把小提琴嗨,Praveen,对我没用,看这把小提琴