我生成了自动生成的字母数字值。当我生成它能够弹出警报,但它无法将值插入文本字段
function stringGen(len)
{
var text = " ";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < len; i++ )
text += charset.charAt(Math.floor(Math.random() * charset.length));
return text;
$('#search').val(stringGen(5));
}
alert(stringGen(5));
答案 0 :(得分:1)
您在最后一个jquery行运行之前返回该函数。尝试在返回之前将其移动,并将文本放在.val中,而不是调用自身内部的函数。
// define the function
function stringGen(len)
{
var text = " ";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < len; i++ )
text += charset.charAt(Math.floor(Math.random() * charset.length));
// put the text in the jquery
$('#search').val(text);
}
// call the function here
stringGen(5);