<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
.textbox
{
margin-right: 5px;
margin-top: 10px;
width: 125px; /*设置输出单元格的长度*/
font-family: 微软雅黑;
text-align: center;
font-weight: bold;
font-size: 16px;
color: Blue;
}
</style>
<script type="text/javascript">
function CreateTextBox() {
var count = GetTextBoxCount();
if (count < 5) {
var textBox = document.createElement("input");
textBox.setAttribute("type", "text");
textBox.setAttribute("name", "myname");
textBox.className = "textbox";
document.body.appendChild(textBox);
} else {
alert("最多生成5个名额");
}
}
function RemoveTextBox() {
var count = GetTextBoxCount();
if (count > 1) {
document.body.removeChild(document.body.lastChild);
}
else {
alert("请最少保留1个名额");
}
}
function GetTextBoxCount() {
var elements = document.getElementsByName("myname");
return elements.length;
}
Array.prototype.Contains = function (num) {
var flag = false;
if (this.length <= 0) {
return flag;
}
for (var i = 0; i < this.length; i++) {
if (this[i] == num) {
flag = true;
break;
}
}
return flag;
}
function CreateRandomNumber(array, count) {
while (array.length < count) {
var TupleValue=new Array();
TupleValue[0]="A";
TupleValue[1]="B";
TupleValue[2]="C";
var TupleValue = Math.floor((Math.random()*TupleValue.length));
if (!array.Contains(TupleValue)) {
array.push(TupleValue);
}
}
}
function SetTextBoxValue() {
var array = new Array();
var count = GetTextBoxCount();
CreateRandomNumber(array, count);
var elements = document.getElementsByName("myname");
for (var i = 0; i < elements.length; i++) {
elements[i].value = array[i];
}
}
</script>
</head>
<body οnlοad="CreateTextBox()">
<input type="button" value="增加" οnclick="CreateTextBox()" />
<input type="button" value="减少" οnclick="RemoveTextBox()" />
<input type="button" value="生成随机数" οnclick="SetTextBoxValue()" /><br />
</body>
</html>
t3
最新推荐文章于 2023-12-17 01:01:52 发布