我只是想知道 setInterval 自动填充字符串后是否可以在输入字段中自动按 Enter 键?
setInterval(function () {
document.getElementById('hudinput').value = Math.random().toString(36).substring(2, 15);
}, 5000);
<div id="hudid" class="hudclass">
<input type="text" name="hud" id="hudinput" class="hud-input" placeholder="Enter text here..." maxlength="140">
</div>
最佳答案
在文本框中按 Enter 键对我来说毫无意义,所以我试图猜测你想要什么。
发送表单
如果你有这样的事情:
<form method="post" id="theForm">
<div id="hudid" class="hudclass">
<input type="text" name="hud" id="hudinput" class="hud-input" placeholder="Enter text here..." maxlength="140">
</div>
</form>
如果您想发送此表格,只需调用 document.getElementById("theForm").submit()
在你的setInterval
功能。
由于您正在使用setInterval
,您可能还想查看ajax
。 jQuery.ajax是一个不错的首选。
追加新行
您是否想在文本框中添加新行?不,<input type="text">
不是多行文本框。您想使用<textarea>
.
如果你想追加一个新行,你可以追加一个新行并获得焦点,而不用按 Enter 键。
setInterval(function () {
document.getElementById('hudinput').value = Math.random().toString(36).substring(2, 15);
document.getElementById('hudinput').value += "\n";
document.getElementById('hudinput').focus();
}, 5000);
<textarea id="hudinput" class="hud-input" placeholder="Enter text here..." maxlength="140" cols="40" rows="5"></textarea>
关于javascript - 如何在设置间隔后在输入字段中按 Enter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51832915/