gpt4 book ai didi

javascript - 字母数字生成的值无法插入文本字段

转载 作者:行者123 更新时间:2023-12-03 07:20:43 25 4
gpt4 key购买 nike

我有自动生成的字母数字值。当我生成它时,它能够在警报中弹出,但无法将值插入文本字段

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));

最佳答案

您将在最后一个 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);

关于javascript - 字母数字生成的值无法插入文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com