当我单击generate id按钮时,如何让它在每次单击时都生成一个唯一的id,并让它显示在旁边的文本框中?假设按钮id是"generateID“,文本框id是"generateidtxt"?它可能全是数字,也可能与字母混在一起。长度可能为8-10个字符..我只需要它每次都是不同的,永远不会重复。

发布于 2013-06-06 03:51:35
如果您正在寻找.net方式,请单击按钮上的just do myTextBox.Text = System.Guid.NewGuid();
NewGuid将为您生成一个新的guid,这在很大程度上保证了每次运行都是唯一的。多个guid http://msdn.microsoft.com/en-us/library/system.guid.aspx
发布于 2013-06-06 03:55:36
document.getElementById("generateID").onclick = function() {
const id = Math.floor(Math.random() * 10000);
document.getElementById("generateID").value = id;
};发布于 2020-04-02 12:44:14
要生成唯一的数字id,Date.now()将提供帮助,要将其设置为文本+数字,请使用toString(Math.floor(Math.random() * 22) + 15) full程序:
const generateID = () =>
Date.now().toString(Math.floor(Math.random() * 20) + 17);
const btnGenerate = document.getElementById('generateID');
const generateIDTXT = document.getElementById('generateidtxt');
const copy = document.getElementById('copy');
btnGenerate.addEventListener('click', () => {
generateIDTXT.value = generateID();
});* {
margin: 0;
padding: 0;
font-family: Arial, Helvetica, sans-serif;
}
button {
background-color: #54fdae;
padding: 5px;
margin: 10px;
border: 0;
border-radius: 3px;
outline: 0;
cursor: pointer;
}
button:hover {
background-color: #32ee9a;
}
input {
border: 0;
border-bottom: 1px solid #000;
padding: 3px;
margin: 10px;
outline: 0;
}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Generate Random ID</title>
</head>
<body>
<button id="generateID">Generate ID</button>
<input type="text" id="generateidtxt" />
</body>
</html>
注意:这是8- 10个单词的数字/字母,这基本上不可能生成两个相同的ids,除非两个人同时按下按钮和,17 - 36之间的随机数是相同的数字
https://stackoverflow.com/questions/16948566
复制相似问题