5

画面、マウス、またはキーボードがないため、フィールドに入力するために Speech-to-Text ソフトウェアを使用しています。このフィールドで 3 秒間入力しないで (実際に話している)、このフォームを送信する必要がありますが、フィールドは「空であってはならない

私はPHPを使用していますが、解決策はJavaScriptまたはjQueryであると思います.この2つはあまり知らないので、使用方法も説明していただければ幸いです.

4

3 に答える 3

5

基本的に、キー押下イベントをキャプチャしてタイマーを開始する必要があります。別のキーが押される前にタイマーが切れた場合は、フォームを送信します

jQuery で押されたキーの検出は次のように行われます。

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

メソッドを使用してsetTimeout()3 秒を計ることができます (ここを参照)

最後に、ここsubmit()に示すように jQuery を使用してフォームを送信します

SR が適切な場所で「入力」できるように、ページがロードされたらテキスト入力にフォーカスを移動するために使用focus()することもできます。

さらに、この記事は、イベントをキャンセルする方法など、JS でイベントのタイミングを設定するための優れた入門書です。

要するに、次のようなものが必要です(テスト済み):

$(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>

(クロージャーのおかげで、これらのTimeout/ vars はまだイベントのスコープ内にあります) ちなみに、これには、最初のキーを押すまでタイマーが開始されないという追加のボーナスがあります。DelayInMs

于 2012-07-24T21:30:17.720 に答える
4
$(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);
});​

デモ

アップデート:

@Tim Downが言ったように、

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.

したがって、キープレスの代わりにキーアップを使用すると、タイマーのクリアがどのキーでも機能します。

于 2012-07-24T21:35:20.530 に答える
0

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
});

これが役立つことを願っています

于 2012-07-24T21:30:54.120 に答える