Col*_*lor 5 html javascript php jquery
我使用语音到文本软件键入一个字段,因为没有屏幕或鼠标或键盘,我需要在此字段中没有打字(实际说话)3秒后发送此表单,但字段不应该'是空的
我正在使用PHP,但我想解决方案是JavaScript或jQuery,我不太了解这两个,所以如果你能解释如何使用它我会很感激.
从根本上说,您需要捕获按键事件并启动计时器.如果在按下另一个键之前计时器用完,请提交表单
检测用jQuery按下的键是这样的:
$('#ElementId').keypress(function() {
/* Your code */
});
Run Code Online (Sandbox Code Playgroud)
您可以使用该setTimeout()
方法计时3秒(参见此处)
最后,使用这里submit()
显示的jQuery 来提交表单
您可能还需要使用focus()
一次页面加载,使SR是"打字",在合适的地方将焦点移到文本输入,
此外,本文是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>
Run Code Online (Sandbox Code Playgroud)
(由于关闭,这些Timeout
/ DelayInMs
vars仍然在事件的范围内)顺便提一下,这还有一个额外的好处,即计时器在第一次按键之后才会启动 - 所以只要你愿意开始说话就可以.
$(function() {\n var time = 0;\n $("textarea").keyup(function() {\n time = 0;\n });\n\n var int = self.setInterval(function() {\n var s = $("textarea").val();\n if (time++ == 3) {\n if (s != "") alert(s);\n time = 0;\n }\n }, 1000);\n});\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n\n\n更新:
\n\n\n\nThe keypress event is designed for handling the a character typed by the user \nrather than detecting keyboard activity and the delete and backspace keys \ndo not generate characters. Some browsers blur this line somewhat but \nthe general principle is that the keyup and keydown events are there to detect \nany key being pressed and telling you about which key it is while keypress is \nfor detecting an actual character being typed.\n
Run Code Online (Sandbox Code Playgroud)\n\n因此,如果您使用 keyup 而不是 keypress,那就更好了,那么您的计时器清除适用于任何键。
\n 归档时间: |
|
查看次数: |
322 次 |
最近记录: |