首页 > 解决方案 > 页面加载时表单中的随机文本

问题描述

嘿,我有一个表单可以在页面加载时自动授予随机数,但无论如何我可以让它授予随机字符,甚至授予数字和字符?

这是我得到的:

document.getElementById('Codefield').value = Math.floor(Math.random() * 1000000000000);
<form method='post' action='secret_code.php'>
  <input type="text" name="" size="40" id="Codefield">
  <input type="submit" class='side' value="Get Code" id="submit" name="submit">
</form>

标签: javascripthtmlforms

解决方案


您可以通过以下代码来完成:

document.getElementById('Codefield').value = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
<form method='post' action='secret_code.php'> 
    <input type="text" name="" size="40" id="Codefield"> 
    <input type="submit" class='side' value="Get Code" id="submit" name="submit"> 
</form>


推荐阅读