设置Interval后如何按输入字段

问题描述 投票:0回答:1

我只是想知道是否有任何方法在setInterval自动填充字符串后自动按输入字段中的回车键?

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>
javascript
1个回答
1
投票

在文本框中按回车对我来说毫无意义,所以我想猜猜你想要什么。

发送表格

如果您有这样的事情:

<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,你可能也想看看ajaxjQuery.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>
© www.soinside.com 2019 - 2024. All rights reserved.