我正在尝试生成一个随机密码(此代码运行良好),该密码将粘贴到密码文本框中的值,但在调用 javascript 函数时遇到问题。
这是我的 JavaScript:
function password() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
var table = document.getElementById('table'),
rows = table.getElementsByTagName('tr'),
i,j, cells, password;
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
for (i=0,j = rows.length; i<j;++i)
{
cells = rows[i].getElementsById('td');
if(!cells.length)
{continue;
}
password = cells[j].innerText;
alert("hi "+password);
}
alert("Password is : "+randomstring);
document.f2.password.value = randomstring;
}
这是我的表:
<table name="table" id="table" cellpadding="4" cellspacing="2" border="1" bgcolor="" >
<tr name="tr" id="tr">
<th><input type="checkbox" name="allCheck" onclick="selectallMe()"></th>
<th>Emp ID</th>
<th>Device</th>
<th>Feature Status</th>
<th>Policy</th>
<th>Password Management</th>
</tr>
<tr>
<% while(rs.next()){ %>
<td><input type="checkbox" name="chkName" onclick="selectall()"></td>
<td><input type="text" name="empId" value="<%= rs.getString(1)%> " disabled="disabled" maxlength="10"></td>
<td><input type="text" name="device" value="<%= rs.getString(2)%>" disabled="disabled" maxlength="10"></td>
<td><input type="text" name="features" value="<%= rs.getString(3)%>" disabled="disabled" maxlength="60"></td>
<td><input type="text" name="policyName" value="<%= rs.getString(4)%>" disabled="disabled" maxlength="10"></td>
<td id="td" name="td"><input type="text" name="password" id="password" value="" ><input type="button" name="Password" value="Password" onclick="password()"><input type="reset" name="Reset" value="Rseset"></td>
</tr>
<% }
%>
</table>
在我的 JS 中出了什么问题?因为我想遍历密码列,Password()
所以将值分配给密码单元格。