广告
广告
广告
html/css/javascript您现在的位置是:主页 > html/css/javascript >

javascript生成随机字符串并赋值给input

2024-01-10 08:35:28html/css/javascript 132人已围观

效果如下图

demo代码


<!DOCTYPE html>
<html>
<head>
	<title>生成随机字符串</title>
</head>
<body>
	<input type="text" id="randomStringInput">
	<button onclick="generateRandomString()">生成随机字符串</button>

	<script>
    function generateRandomString() {
		const length = 10; // 随机字符串的长度
		const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
		const randomString = Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length)));
		document.getElementById('randomStringInput').value = randomString.join('');
    }
	</script>
</body>
</html>


 


扫码添加技术微信【解决问题,仅需10元起】

专注中小企业网站建设、网站安全15年。
熟悉各种CMS,精通PHP+MYSQL、HTML5、CSS3、Javascript等。
承接:企业仿站、网站修改、网站改版、BUG修复、问题处理、二次开发、PSD转HTML、网站被黑、网站漏洞修复等。
专业解决各种疑难杂症,您有任何网站问题都可联系我们技术人员微信。
➥ 可淘宝担保交易,安全无风险

广告
广告
技术微信