【问题标题】:Send form after X seconds of not typing in specific field未在特定字段中输入 X 秒后发送表单
【发布时间】:2012-07-25 19:45:37
【问题描述】:

我正在使用 Speech-to-Text 软件输入字段,因为没有屏幕或鼠标或键盘,我需要在此字段中没有输入(实际上是说话)3 秒后发送此表单,但是字段不应为空

我正在使用 PHP,但我想解决方案是 JavaScript 或 jQuery,我对这两个不太了解,所以如果您也能解释一下如何使用它,我将不胜感激。

【问题讨论】:

  • 你想让我们教你javascript吗?
  • 谢谢你们,我最终使用了这个 $(function() { var time = 0; $("textarea").keypress(function() { time = 0; }); var int = self.setInterval(function() { var s = $("textarea").val(); if (time++ == 3) { if (s != "") document.forms["form"].submit() ; 时间 = 0; } }, 1000); });
  • @user1549838 如果某人的回答有帮助,请单击他们问题旁边的勾号,这将奖励他们代表。我怀疑您应该根据您提到的代码奖励@ocanal。另外,不要忘记您可以编辑您的问题以添加更多信息。如果做不到这一点,您可以在问题/评论中标记代码,方法是用反引号 (```)
  • 好吧,我不知道,我是新来的,我就是这么做的

标签: php javascript jquery html


【解决方案1】:

基本上,您需要捕获按键事件并启动计时器。如果计时器在按下另一个键之前用完,请提交表单

Detecting key pressed with jQuery 是这样完成的:

$('#ElementId').keypress(function() {
    /* Your code */
});

你可以使用setTimeout()方法来计时3秒(See here

最后,使用jQuery的submit()如图here提交表单

您可能还想use focus() 在页面加载后将焦点移至文本输入,以便 SR 在正确的位置“键入”,

另外,this article 很好地介绍了 JS 中的计时事件,包括如何取消它们

简而言之,你想要这样的东西 (tested):

$(document).ready(function(){

    var Timeout; //For reference to timeout
    var DelayInMs=3000;
    $("#SRInput").focus(); //Give focus to input on document ready

    //When input is received, (re)set the timer
    $("#SRInput").keypress(function() {
        if(Timeout) {clearTimeout(Timeout);} //Clear existing timeout, if any
        Timeout = setTimeout(function(){$("#SRForm").submit();}, DelayInMs);
    });

});

<form id="SRForm" method = ... >
    <input type="text" name="SRInput" id="SRInput"/>
</form>

(感谢closures,这些Timeout/DelayInMs 变量仍在活动范围内) 顺便说一句,这还有一个额外的好处,即计时器在第一次按键之后才开始 - 所以你可以花多长时间来开始说话)。

【讨论】:

  • 您的回答没有说​​明如何在新按键时重置计时器;当输入失去焦点时也会触发 jQuery change() 事件,这不是我们想要的。
  • 谢谢你的详细回复我的朋友你们太棒了
  • @user1549838 不客气。希望我们能再次见到你
【解决方案2】:
$(function() {
    var time = 0;
    $("textarea").keyup(function() {
        time = 0;
    });

    var int = self.setInterval(function() {
        var s = $("textarea").val();
        if (time++ == 3) {
            if (s != "") alert(s);
            time = 0;
        }
    }, 1000);
});​

DEMO

更新:

As @Tim Down has said,

The keypress event is designed for handling the a character typed by the user 
rather than detecting keyboard activity and the delete and backspace keys 
do not generate characters. Some browsers blur this line somewhat but 
the general principle is that the keyup and keydown events are there to detect 
any key being pressed and telling you about which key it is while keypress is 
for detecting an actual character being typed.

因此,如果您使用 keyup 而不是 keypress 会更好,然后您的计时器清除适用于任何键。

【讨论】:

  • 您可能需要稍微调整一下时间,因为这可能会在 2 到 3 秒之间发生任何变化。除此之外,一个不错的方法
  • 这实际上只需添加 document.forms["form"].submit();非常感谢 ocanal,也非常感谢 Basic(我试过你的代码,但对我来说太复杂了,我无法运行它)和 Corennekm
【解决方案3】:

我认为你可以使用http://narf.pl/jquery-typing/jquery.typing-0.2.0.min.js

看看http://narf.pl/jquery-typing/

$(':text').typing({
    start: function (event, $elem) {
       // do nothing
    },
    stop: function (event, $elem) {
       // send your form
    },
    delay: 3000
});

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多