我只是想知道是否有任何方法在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>
在文本框中按回车对我来说毫无意义,所以我想猜猜你想要什么。
如果您有这样的事情:
<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>