【问题标题】:Auto submit form after x mili seconds of inactive keyboard非活动键盘 x 毫秒后自动提交表单
【发布时间】:2010-11-05 10:54:50
【问题描述】:

在我的表单中,我有一组输入框,用户可以在其中输入值。 更改其中一个框时,表单会自动提交。

然而,现在的问题是用户停留在最后一个字段中,拿起鼠标并按下 OK 按钮(另一个表单),而没有先离开文本框。更改事件不会被触发,旧的、不正确的值会被传递到下一页。

我想在键盘处于非活动状态几毫秒后触发 onchange 事件。就像大多数自动完成插件一样。
我想我可以实现一个计时器,它在您输入输入字段的那一刻开始计时,并在每次处理击键时重置,然后当它达到零时触发 onchange 事件。

我不打算重新发明轮子,我想知道这样的功能是否在某个地方可用。
有什么建议吗?

【问题讨论】:

    标签: javascript jquery html timer onchange


    【解决方案1】:

    我遇到了类似的问题,并创建了一个当前在内部应用程序中使用的 jQuery 插件。它应该在用户完成输入后触发更改事件。

    如果您不使用 jQuery,代码仍然适用于其他任何内容。

    jQuery.fn.handleKeyboardChange = function(nDelay)
    {
        // Utility function to test if a keyboard event should be ignored
        function shouldIgnore(event) 
        { 
            var mapIgnoredKeys = {
                 9:true, // Tab
                16:true, 17:true, 18:true, // Shift, Alt, Ctrl
                37:true, 38:true, 39:true, 40:true, // Arrows 
                91:true, 92:true, 93:true // Windows keys
            };
            return mapIgnoredKeys[event.which];
        }
    
        // Utility function to fire OUR change event if the value was actually changed
        function fireChange($element)
        {
            if( $element.val() != jQuery.data($element[0], "valueLast") )
            {
                jQuery.data($element[0], "valueLast", $element.val())
                $element.trigger("change");
            }
        }
    
        // The currently running timeout,
        // will be accessed with closures
        var timeout = 0;
    
        // Utility function to cancel a previously set timeout
        function clearPreviousTimeout()
        {
            if( timeout )
            { 
                clearTimeout(timeout);
            }
        }
    
        return this
        .keydown(function(event)
        {
            if( shouldIgnore(event) ) return;
            // User pressed a key, stop the timeout for now
            clearPreviousTimeout();
            return null; 
        })
        .keyup(function(event)
        {
            if( shouldIgnore(event) ) return;
            // Start a timeout to fire our event after some time of inactivity
            // Eventually cancel a previously running timeout
            clearPreviousTimeout();
            var $self = $(this);
            timeout = setTimeout(function(){ fireChange($self) }, nDelay);
        })
        .change(function()
        {
            // Fire a change
            // Use our function instead of just firing the event
            // Because we want to check if value really changed since
            // our previous event.
            // This is for when the browser fires the change event
            // though we already fired the event because of the timeout
            fireChange($(this));
        })
        ;
    }
    

    用法:

    $("#my_input").handleKeyboardChange(300).change(function()
    {
        // value has changed!
    });
    

    【讨论】:

    • 不错。你考虑到了一些我没有考虑到的情况
    • 我确实改变了 $element.trigger("Keyboard.change");到 $element.trigger("change");比我幼稚的实现要好得多。
    • 抱歉,这是针对这个特定应用程序的命名空间事件。您更改它是对的,我将更改它上面的代码。
    • 对我来说,在 safari 上,一旦您在输入后单击框外就会触发该功能
    【解决方案2】:

    做一个onBlur是不是不行,所以无论是当用户移动到下一个字段还是点击别的东西时,值都会被保存?

    【讨论】:

    • 当用户单击使用当前值的提交按钮转到另一个页面时会出现问题。在提交进入下一页的表单之前,没有时间先重新提交之前的表单。
    【解决方案3】:

    我不知道这样的解决方案会被视为“重新发明”任何东西。正如您所说,一旦页面加载,这听起来只不过是一个简单的 setTimeout 。大约 3,000 毫秒后,它运行 form.submit()。

    我也可能会在每次击键时重新开始倒计时,以便给用户足够的时间进行输入。

    【讨论】:

    • 你是对的,我做到了。我只是害怕按键事件中的浏览器差异。不过到目前为止没有问题。
    猜你喜欢
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    相关资源
    最近更新 更多