我需要使用for循环将2个随机字母添加到文本框中的字符串中,并在单击按钮时在加密的字符串框中返回。因此,例如,如果输入cat,它可能会像cynarwtpp一样返回。我是for循环的新手,不确定如何从这里开始,任何帮助将不胜感激。
<script type="text/javascript">
var uncoded_array = uncoded.split("");
var coded_str = "";
var alphabet = new Array("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");
</script>
<form action="">
Enter a String: <input type="text" name="uncoded" ></br>
<input type="button" value="cipher" onClick=document.forms[0].coded.value= ></br>
Encrypted String: <input type="text" name="coded" ></br>
答案 0 :(得分:1)
function makecoded()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
这对你有用,会给你一些随机的字符串,然后你可以把它添加到任何地方