jquery实现 字母(大小写)数字生成随机密码

来自:互联网
时间:2022-01-25
阅读:

代码如下:

<script src="http://libs.bAIdu.com/jquery/1.8.3/jquery.min.js"></script>//在线链接
<script type="text/JavaScript">
$(document).ready(function() {
$('#get_pwd').click(function(){
var str = 'mnbvcxzasdfghj0123klpoiuytrewq678MNBVCXZLKJH45GFDSAPOIUYT9REWQ';//字符串 
var len = str.length;//查询字符串长度 
var num=8; //生成字符串长度
var code = '';
for (var i = 0; i < num; i++) {
var rand = Math.ceil(Math.random()*len - 1);
//console.log(rand)
code+= str[rand];
}
$('#user_pws').val(code);//写入指定 input 框
});
})
</script>

使用方法:

//例 
<input type="button" value="随机密码" id="get_pwd" style="cursor: pointer;"/>
返回顶部
顶部