gpt4 book ai didi

javascript - 如何在 JavaScript 中生成随机数并将其分配给我的变量数组?

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

我需要创建一个神奇的 8 球网页,当输入问题并单击按钮时,该网页会生成随机响应。到目前为止,我已经完成了 HTML 和 CSS:

$(document).ready(function() {
$("button").click(function() {
var answer;
var my_num = getRandom(14);
var responses = Array(15);
responses[0] = "Ask again later...";
responses[1] = "Yes";
responses[2] = "No";
responses[3] = "It appears to be so";
responses[4] = "Reply is hazy, please try again";
responses[5] = "Yes, definitely";
responses[6] = "What is it you really want to know?";
responses[7] = "Outlook is good";
responses[8] = "My sources say no";
responses[9] = "Signs point to yes";
responses[10] = "Don't count on it";
responses[11] = "Cannot predict now";
responses[12] = "As I see it, yes";
responses[13] = "Better not tell you now";
responses[14] = "Concentrate and ask again";

var RandNum = Math.floor(Math.random() * 14);

var answer = responses[RandNum];

$("#Response").replaceWith(answer);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Magic 8 Ball</title>
<link rel="stylesheet" href="styles.css">
</head>

<body>
<div id="wrapper">
<header>
<h1>Magic 8 Ball</h1>
</header>
<h3>What would you like to know?</h3>
<input type="text" name="txtQuestion" id="txtQuestion">
<br />
<input type="button" id="btnAsk" value="Ask the 8 Ball">
<h3>The 8 Ball says:</h3>
<h3 id="Response">Ask the 8 Ball a question...</h3>
</div>
<script src="scripts/jquery-3.2.1.js"></script>
<script src="scripts/my_scripts.js"></script>
</body>

</html>

我一直在研究 JavaScript,但我所做的一切似乎都没有产生任何东西!这是我的代码:

说实话,我不知道自己做错了什么。我想我应该创建一个包含所有可能响应的新数组。然后我生成一个随机数,并将其分配给数组变量以给出随机响应。然后我用我随机生成的响应替换了存在的 h3 标签。理论上听起来不错。然而,当我点击按钮时,没有任何效果。即使我手动分配一个响应,例如答案=响应[5],也不会发生任何事情。我在这里缺少什么?

最佳答案

您的 html 中没有按钮元素,因此您必须将事件附加到 "#btnAsk" 并且 var my_num = getRandom(14) 也会给出错误,因为 getRandom( ) 尚未定义,也不是必需的。

$(document).ready(function() {
$("#btnAsk").click(function() {
var answer;

var responses = Array(15);
responses[0] = "Ask again later...";
responses[1] = "Yes";
responses[2] = "No";
responses[3] = "It appears to be so";
responses[4] = "Reply is hazy, please try again";
responses[5] = "Yes, definitely";
responses[6] = "What is it you really want to know?";
responses[7] = "Outlook is good";
responses[8] = "My sources say no";
responses[9] = "Signs point to yes";
responses[10] = "Don't count on it";
responses[11] = "Cannot predict now";
responses[12] = "As I see it, yes";
responses[13] = "Better not tell you now";
responses[14] = "Concentrate and ask again";

var RandNum = Math.floor(Math.random() * 14);

var answer = responses[RandNum];

$("#Response").html(answer);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Magic 8 Ball</title>
<link rel="stylesheet" href="styles.css">
</head>

<body>
<div id="wrapper">
<header>
<h1>Magic 8 Ball</h1>
</header>
<h3>What would you like to know?</h3>
<input type="text" name="txtQuestion" id="txtQuestion">
<br />
<input type="button" id="btnAsk" value="Ask the 8 Ball">
<h3>The 8 Ball says:</h3>
<h3 id="Response">Ask the 8 Ball a question...</h3>
</div>
<script src="scripts/jquery-3.2.1.js"></script>
<script src="scripts/my_scripts.js"></script>
</body>

</html>

关于javascript - 如何在 JavaScript 中生成随机数并将其分配给我的变量数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46374767/

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