Alpha数字生成的值无法插入到texfield中

时间:2016-03-26 00:03:26

标签: javascript jquery

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

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

1 个答案:

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