我想让一家餐馆选择节目。它需要一个字符串数组,使用随机函数,然后将选项显示在表单文本框中。
我无法让它发挥作用。我一直在工作4个小时。 JavaScript工作正常,但我无法让它显示在文本框中。
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Generate Restaurant</button>
<script>
var food = ["Molinas", "Sonic", "Subway", "McDonalds", "Taco Bell","Pasanos", "Lone Star Eatery", "Chicken Express"];
function myFunction() {
var x = Math.floor((Math.random() * 8) + 0);
document.getElementById("demo").innerHTML = food[x];
}
</script>
<form>
<input id="demo" type="text">
</form>
</body>
</html>
答案 0 :(得分:-1)
这会是一种更好的方法吗?
<!DOCTYPE html>
<html>
<body>
<script>
var food = ["Molinas", "Sonic", "Subway", "McDonalds", "Taco Bell", "Pasanos", "Lone Star Eatery", "Chicken Express"];
function myFunction() {
var x = Math.floor((Math.random() * 8) + 0);
return food[x];
}
</script>
<form onsubmit="myFunction()">
Enter name: <input type="text">
<input type="submit">
</form>
</body>
</html>