我正在尝试创建一个随机的用户名生成器,当用户单击“ gen-button”时,应该为他们提供一个随机的用户名。我是javascript新手,因此将不胜感激:)
var nameList = [
'Time', 'Past', 'Future', 'Dev',
'Fly', 'Flying', 'Soar', 'Soaring', 'Power', 'Falling',
'Fall', 'Jump', 'Cliff', 'Mountain', 'Rend', 'Red', 'Blue',
'Green', 'Yellow', 'Gold', 'Demon', 'Demonic', 'Panda', 'Cat',
'Kitty', 'Kitten', 'Zero', 'Memory', 'Trooper', 'XX', 'Bandit',
'Fear', 'Light', 'Glow', 'Tread', 'Deep', 'Deeper', 'Deepest',
'Mine', 'Your', 'Worst', 'Enemy', 'Hostile', 'Force', 'Video',
'Game', 'Donkey', 'Mule', 'Colt', 'Cult', 'Cultist', 'Magnum',
'Gun', 'Assault', 'Recon', 'Trap', 'Trapper', 'Redeem', 'Code',
'Script', 'Writer', 'Near', 'Close', 'Open', 'Cube', 'Circle',
'Geo', 'Genome', 'Germ', 'Spaz', 'Shot', 'Echo', 'Beta', 'Alpha',
'Gamma', 'Omega', 'Seal', 'Squid', 'Money', 'Cash', 'Lord', 'King',
'Duke', 'Rest', 'Fire', 'Flame', 'Morrow', 'Break', 'Breaker', 'Numb',
'Ice', 'Cold', 'Rotten', 'Sick', 'Sickly', 'Janitor', 'Camel', 'Rooster',
'Sand', 'Desert', 'Dessert', 'Hurdle', 'Racer', 'Eraser', 'Erase', 'Big',
'Small', 'Short', 'Tall', 'Sith', 'Bounty', 'Hunter', 'Cracked', 'Broken',
'Sad', 'Happy', 'Joy', 'Joyful', 'Crimson', 'Destiny', 'Deceit', 'Lies',
'Lie', 'Honest', 'Destined', 'Bloxxer', 'Hawk', 'Eagle', 'Hawker', 'Walker',
'Zombie', 'Sarge', 'Capt', 'Captain', 'Punch', 'One', 'Two', 'Uno', 'Slice',
'Slash', 'Melt', 'Melted', 'Melting', 'Fell', 'Wolf', 'Hound',
'Legacy', 'Sharp', 'Dead', 'Mew', 'Chuckle', 'Bubba', 'Bubble', 'Sandwich', 'Smasher', 'Extreme', 'Multi', 'Universe', 'Ultimate', 'Death', 'Ready', 'Monkey', 'Elevator', 'Wrench', 'Grease', 'Head', 'Theme', 'Grand', 'Cool', 'Kid', 'Boy', 'Girl', 'Vortex', 'Paradox'
];
var finalName = ""
function generate() {
document.getElementById("gen-button").innerHTML = finalName();
finalName = nameList[Math.floor(Math.random() * nameList.length)];
return finalName;
};
<input id="uNameInput" class="choosename" type="text" maxlength="12" minlength="4" placeholder="username" required>
<input onclick="generate()" id="gen-button" class="modern" type="button" value="Generate usename" </input>
答案 0 :(得分:4)
如控制台finalName
中所述,此功能无效。变量finalName
仅包含具有随机名称的字符串。
然后,您需要更改输入value
的{{1}}属性。
uNameInput
var nameList = [
'Time','Past','Future','Dev',
'Fly','Flying','Soar','Soaring','Power','Falling',
'Fall','Jump','Cliff','Mountain','Rend','Red','Blue',
'Green','Yellow','Gold','Demon','Demonic','Panda','Cat',
'Kitty','Kitten','Zero','Memory','Trooper','XX','Bandit',
'Fear','Light','Glow','Tread','Deep','Deeper','Deepest',
'Mine','Your','Worst','Enemy','Hostile','Force','Video',
'Game','Donkey','Mule','Colt','Cult','Cultist','Magnum',
'Gun','Assault','Recon','Trap','Trapper','Redeem','Code',
'Script','Writer','Near','Close','Open','Cube','Circle',
'Geo','Genome','Germ','Spaz','Shot','Echo','Beta','Alpha',
'Gamma','Omega','Seal','Squid','Money','Cash','Lord','King',
'Duke','Rest','Fire','Flame','Morrow','Break','Breaker','Numb',
'Ice','Cold','Rotten','Sick','Sickly','Janitor','Camel','Rooster',
'Sand','Desert','Dessert','Hurdle','Racer','Eraser','Erase','Big',
'Small','Short','Tall','Sith','Bounty','Hunter','Cracked','Broken',
'Sad','Happy','Joy','Joyful','Crimson','Destiny','Deceit','Lies',
'Lie','Honest','Destined','Bloxxer','Hawk','Eagle','Hawker','Walker',
'Zombie','Sarge','Capt','Captain','Punch','One','Two','Uno','Slice',
'Slash','Melt','Melted','Melting','Fell','Wolf','Hound',
'Legacy','Sharp','Dead','Mew','Chuckle','Bubba','Bubble','Sandwich','Smasher','Extreme','Multi','Universe','Ultimate','Death','Ready','Monkey','Elevator','Wrench','Grease','Head','Theme','Grand','Cool','Kid','Boy','Girl','Vortex','Paradox'
];
var finalName = ""
function generate() {
var finalName = nameList[Math.floor( Math.random() * nameList.length )];
document.getElementById("uNameInput").value = finalName;
};
答案 1 :(得分:1)
首先,input
个元素没有结束标记。
接下来,您尝试将.innerHTML
分配给返回值finalName
,此时返回值在您的代码中为空字符串,从而导致finalName is not a function
错误。尽管如此,input
元素没有任何.innerHTML
,它们具有value
属性,因此,您需要做的就是分配{{1}的value
}到随机运算的结果。
此外,您不应使用内联HTML事件属性,例如input
。有a bunch of reasons不使用这种已有20多年历史的技术,它不会死于应有的死亡。相反,请遵循现代标准,将HTML与JavaScript分开,然后使用.addEventListener()
注册DOM事件。
最后一项(它比其他任何事情更多地是UI设计建议),因为您将要填充该字段,因此您似乎不太希望用户能够更改随机分配的值。如果是这种情况,请首先不要使用任何形式的表单字段。您可以使用常规的DOM元素,例如onclick
,在这种情况下,您只需将<span>
的{{1}}设置为随机名称,而不是.textContent
span
中的一个。我已经将该示例添加到了答案中。
value
input
var nameList = [
'Time','Past','Future','Dev',
'Fly','Flying','Soar','Soaring','Power','Falling',
'Fall','Jump','Cliff','Mountain','Rend','Red','Blue',
'Green','Yellow','Gold','Demon','Demonic','Panda','Cat',
'Kitty','Kitten','Zero','Memory','Trooper','XX','Bandit',
'Fear','Light','Glow','Tread','Deep','Deeper','Deepest',
'Mine','Your','Worst','Enemy','Hostile','Force','Video',
'Game','Donkey','Mule','Colt','Cult','Cultist','Magnum',
'Gun','Assault','Recon','Trap','Trapper','Redeem','Code',
'Script','Writer','Near','Close','Open','Cube','Circle',
'Geo','Genome','Germ','Spaz','Shot','Echo','Beta','Alpha',
'Gamma','Omega','Seal','Squid','Money','Cash','Lord','King',
'Duke','Rest','Fire','Flame','Morrow','Break','Breaker','Numb',
'Ice','Cold','Rotten','Sick','Sickly','Janitor','Camel','Rooster',
'Sand','Desert','Dessert','Hurdle','Racer','Eraser','Erase','Big',
'Small','Short','Tall','Sith','Bounty','Hunter','Cracked','Broken',
'Sad','Happy','Joy','Joyful','Crimson','Destiny','Deceit','Lies',
'Lie','Honest','Destined','Bloxxer','Hawk','Eagle','Hawker','Walker',
'Zombie','Sarge','Capt','Captain','Punch','One','Two','Uno','Slice',
'Slash','Melt','Melted','Melting','Fell','Wolf','Hound',
'Legacy','Sharp','Dead','Mew','Chuckle','Bubba','Bubble','Sandwich',
'Smasher','Extreme','Multi','Universe','Ultimate','Death','Ready','Monkey', 'Elevator','Wrench','Grease','Head','Theme','Grand','Cool','Kid','Boy',
'Girl','Vortex','Paradox'
];
// Get a reference to the element that needs an event handler
// and the element(s) that needs the output
let btn = document.getElementById("gen-button");
let output = document.getElementById("uNameInput");
let output2 = document.querySelector(".randomName");
// Set up the event handler
btn.addEventListener("click", generate);
function generate() {
// The function doesn't need to "return" anything. It just needs
// to set the value of the text field to the random name.
let result = nameList[Math.floor( Math.random() * nameList.length )];
output.value = result; // <-- form fields have a value
output2.textContent = result; // non-form elements have textContent
};
答案 2 :(得分:0)
以下是您犯过的所有错误的列表:
innerHTML
,您应该改为更改value
属性finalName()
而不是finalName
来分配名称(fieldName
是string
,而不是function
)finalName
之前分配了值,因此您将值设置为空字符串(或先前选择的名称)而不是新名称<input>
或<input />
,而不是通常的<div></div>
finalName
,因为它没有用(即使您要存储名称,也可以从输入值中获取它)generate
函数声明之后添加了不需要添加的分号。使用function expression
(var generate = function(){};
)“声明”分号时,只需要在函数声明中添加分号即可。这是您的代码的有效版本:
var nameList = [
'Time', 'Past', 'Future', 'Dev',
'Fly', 'Flying', 'Soar', 'Soaring', 'Power', 'Falling',
'Fall', 'Jump', 'Cliff', 'Mountain', 'Rend', 'Red', 'Blue',
'Green', 'Yellow', 'Gold', 'Demon', 'Demonic', 'Panda', 'Cat',
'Kitty', 'Kitten', 'Zero', 'Memory', 'Trooper', 'XX', 'Bandit',
'Fear', 'Light', 'Glow', 'Tread', 'Deep', 'Deeper', 'Deepest',
'Mine', 'Your', 'Worst', 'Enemy', 'Hostile', 'Force', 'Video',
'Game', 'Donkey', 'Mule', 'Colt', 'Cult', 'Cultist', 'Magnum',
'Gun', 'Assault', 'Recon', 'Trap', 'Trapper', 'Redeem', 'Code',
'Script', 'Writer', 'Near', 'Close', 'Open', 'Cube', 'Circle',
'Geo', 'Genome', 'Germ', 'Spaz', 'Shot', 'Echo', 'Beta', 'Alpha',
'Gamma', 'Omega', 'Seal', 'Squid', 'Money', 'Cash', 'Lord', 'King',
'Duke', 'Rest', 'Fire', 'Flame', 'Morrow', 'Break', 'Breaker', 'Numb',
'Ice', 'Cold', 'Rotten', 'Sick', 'Sickly', 'Janitor', 'Camel', 'Rooster',
'Sand', 'Desert', 'Dessert', 'Hurdle', 'Racer', 'Eraser', 'Erase', 'Big',
'Small', 'Short', 'Tall', 'Sith', 'Bounty', 'Hunter', 'Cracked', 'Broken',
'Sad', 'Happy', 'Joy', 'Joyful', 'Crimson', 'Destiny', 'Deceit', 'Lies',
'Lie', 'Honest', 'Destined', 'Bloxxer', 'Hawk', 'Eagle', 'Hawker', 'Walker',
'Zombie', 'Sarge', 'Capt', 'Captain', 'Punch', 'One', 'Two', 'Uno', 'Slice',
'Slash', 'Melt', 'Melted', 'Melting', 'Fell', 'Wolf', 'Hound',
'Legacy', 'Sharp', 'Dead', 'Mew', 'Chuckle', 'Bubba', 'Bubble', 'Sandwich', 'Smasher', 'Extreme', 'Multi', 'Universe', 'Ultimate', 'Death', 'Ready', 'Monkey', 'Elevator', 'Wrench', 'Grease', 'Head', 'Theme', 'Grand', 'Cool', 'Kid', 'Boy', 'Girl', 'Vortex', 'Paradox'
];
document.querySelector("#gen-button").addEventListener("click", generate);
function generate() {
return document.querySelector("#uNameInput").value = nameList[Math.floor(Math.random() * nameList.length)];
}
<input id="uNameInput" class="choosename" type="text" maxlength="12" minlength="4" placeholder="username" required>
<input id="gen-button" class="modern" type="button" value="Generate usename" />