1
下面我有一个javascript函数:我要显示我在文本文本框中字符串,但它没有这样做
var alphabet = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ];
function generateRandomString(strLength) {
var output = "";
for (var i = 0; i < strLength; i++) {
var randomIndex = Math.floor(Math.random() * alphabet.length);
output += alphabet[randomIndex]; }
return output; }
function getSession() {
var randomDiv = document.getElementById("randomStrings");
newText = generateRandomString(3);
randomDiv.innerHTML = newText;
}
什么这个函数的作用是获取任何字母的3倍,例如显示它AGH,TRY,MNK等
这被显示在一个段落象下面这样:
<p id="randomStrings"></p>
上面的代码并实际成功地在屏幕上显示的代码。但是因为我想将这些数据发布到另一个页面中,我认为最好不要使用<p>
标签来显示随机字符串,而是使用测试框来显示它。问题是,如果我这样做,它不会在文本框中显示randomString。为什么在文本框中不显示randomString?
下面是表单代码:
<form action="QandATable.php" method="post" id="sessionForm">
<table>
<tr>
<th>1: </th>
<td><input id="sessionBtn" type="button" value="Get Session ID" name="sessionid" onClick="getSession()" /></td> <!-- Get Session ID here-->
<td><input type="text" id="randomStrings" name="random" readonly="readonly"/></td>
</tr>
</table>
</form>
您必须设置值的输入,而不是innertHTML –
我不明白,事情怎么会在teztbox显示,更不用说随机字符串。它没有指定任何值。 – robert
生成服务器端不是更有意义吗? –