0

我试图通过 JavaScript 在警报中显示 5 个随机字母或数字。

当我跑步时,警报没有出现。

抱歉,我是 JavaScript 新手,任何帮助都会很棒。谢谢。

<script language="javascript" type="text/javascript">
 function randomString() {
   var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
   var string_length = 8;
   var randomstring = '';
   for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
 }
  document.randform.randomfield.value = randomstring;
}
</script>

<script>
 function myFunction(){
   alert(randomString());
}
</script>

我正在使用这个按钮来调用它。

<input type="button" onclick="myFunction()" value="Show alert box">
4

3 回答 3

1

放:

return randomstring;

randomString函数的最后。

于 2013-04-23T05:52:07.490 回答
1

返回 的值randomstring

function randomString() {
   var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
   var string_length = 8;
   var randomstring = '';
   for (var i=0; i<string_length; i++) {
       var rnum = Math.floor(Math.random() * chars.length);
       randomstring += chars.substring(rnum,rnum+1);
   }
   return randomstring;
}
于 2013-04-23T05:52:19.043 回答
0
<html>
<head>
<script type="text/javascript">
function alertUser(msg) {
alert(msg);
}
</script>
</head>
<body onload="alertUser('Welcome to this AMAZING web page!')">
<div style="text-align:center;padding:10px;">
<h1>Amazing Web Page.</h1>
<p><a href="JavaScript:self.close();">Close</a></p>
</div>
</body>
</html>
于 2013-04-23T06:00:59.060 回答