我正在构建一个简单的随机字生成器,并尝试使用<select>
下拉列表确定要显示的字数。我的想法是让<option>
的值包含我想要解析到我的函数中的变量。但是,它并没有像我希望的那样读取变量,就像在第一个div中那样,而是写出文字值。我确信这是一个更优雅的方式来写这个,但现在我很好奇,如果我甚至可以这样做。
HTML:
<select id="wordCount" class="span2">
<option value="word1">1 Words</option>
<option value="word1+' '+word2">2 Words</option>
<option value="word1+' '+word2+' '+word3">3 Words</option>
<option value="word1+' '+word2+' '+word3+' '+word4">4 Words</option>
</select>
<input id="gen" type="submit" value="Generate">
<div id="wordBin"></div>
<div id="wordBin2"></div>
JavasSript:
$('#gen').click(function generateWords(){
var wordCount = document.getElementById('wordCount').value;
var wordbank = ['rock', 'paper', 'scissor', 'apple', 'beer', 'potato'];
var word1 = wordbank[Math.floor(Math.random()*wordbank.length)];
var word2 = wordbank[Math.floor(Math.random()*wordbank.length)];
var word3 = wordbank[Math.floor(Math.random()*wordbank.length)];
var word4 = wordbank[Math.floor(Math.random()*wordbank.length)];
var wordBin = document.getElementById('wordBin');
var wordBin2 = document.getElementById('wordBin2');
wordBin.innerHTML = word1+' '+word2+' '+word3+' '+word4;
wordBin2.innerHTML = wordCount;
});
答案 0 :(得分:2)
我根本不推荐这个,但你要找的是eval
。我通过一个简单的改变更新了小提琴:
wordBin.innerHTML = word1+' '+word2+' '+word3+' '+word4;
到
wordBin.innerHTML = eval(wordCount);
它就像你想要的那样工作。但是,这是一种 TERRIBLE 做事的方式(就像你提到的那样)。如果你在你的值中存储一个计数并通过附加随机单词循环计数会好得多。真正的HTML / javascript会像这样工作:
<强> HTML:强>
<select id="wordCount" class="span2">
<option value="1">1 Words</option>
<option value="2" selected="selected">2 Words</option>
<option value="3">3 Words</option>
<option value="4">4 Words</option>
</select>
<input id="gen" type="submit" value="Generate">
<div id="wordBin"></div>
<div id="wordBin2"></div>
<强> JS:强>
$('#gen').click(function generateWords() {
var wordCount = document.getElementById('wordCount').value;
var wordbank = ['rock', 'paper', 'scissor', 'apple', 'beer', 'potato'];
var finalMessage = "";
for (i = 0; i < parseInt(wordCount, 10); i++) {
if (i > 0) finalMessage += " ";
finalMessage += wordbank[Math.floor(Math.random() * wordbank.length)];
}
var wordBin = document.getElementById('wordBin');
var wordBin2 = document.getElementById('wordBin2');
wordBin.innerHTML = finalMessage;
wordBin2.innerHTML = wordCount;
});
更新小提琴: http://jsfiddle.net/N988s/2/
答案 1 :(得分:1)
为什么不这样:
<select id="wordCount" class="span2">
<option value="1">1 Word</option>
<option value="2">2 Words</option>
<option value="3">3 Words</option>
<option value="4">4 Words</option>
</select>
<input id="gen" type="submit" value="Generate">
<div id="wordBin"></div>
<div id="wordBin2"></div>
JS:
var wordbank = ['rock', 'paper', 'scissor', 'apple', 'beer', 'potato'];
$('#gen').click(function () {
var wordCount = document.getElementById('wordCount').value;
var wordBin = document.getElementById('wordBin');
words = '';
for (var i = 0; i < wordCount; i++) {
if (words != '') words += ' ';
words += getRandomWord();
wordBin.innerHTML = words;
}
});
function getRandomWord() {
return wordbank[Math.floor(Math.random()*wordbank.length)];
}