gpt4 book ai didi

javascript - 根据下拉选择检索数据

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

我正在开发一个通过 API 生成问题的问答应用程序。单击“获取问题”按钮后,我希望生成的问题适用于所选的类别/难度。现在它在页面加载时在控制台上生成随机问题。我不清楚哪里出错了?

到目前为止我的代码是这样的:

const score = document.querySelector('#score');
const output = document.querySelector('#output');
const answerSelect = document.querySelector('#selAnswers');
const btn = document.querySelector('#btn');

const categories = [
'Sports',
'Art',
'Vehicles',
'History'
];

const difficulty = [
'easy',
'medium',
'hard'
];

btn.addEventListener('submit', function(e) {
e.preventDefault();

});

document.addEventListener('DOMContentLoaded', function() {

let categorySelect = document.querySelector('#category');
let difficultySelect = document.querySelector('#difficulty');

let html = '';
for (let item of categories) {
// Appending Strings created using Template literals
html += `<option>${item}</option>`;
}
categorySelect.innerHTML = html;

for (let item of difficulty) {
let opt = document.createElement('option');

// <option> will use its .textContent as value if not explicitly defined
opt.textContent = item;

difficultySelect.append(opt);
}
});

$.ajax ({
url: 'https://opentdb.com/api.php?amount=10',
data: '{}',
type: 'GET',
dataType: 'json',
success: function(data) {
console.log(data.results[0]);


data.items.forEach(function(item){


//console.log(item);

let output = getOutput(item);
//Display Results

result.innerHTML += output;
});

},
error: function(jqXHR, textStatus, ex ){
console.log(`${textStatus}, ${ex}, ${jqXHR.responseText}`);
alert(`${textStatus}, ${ex} ${jqXHR.responseText}`);
}


});


//function

function getQuestion(){

result.innerHTML ='';
btn.innerHTML ='';

let categoryOption = categories.value;
let difficultyLevel = difficulty.value;
}

HTML:

<!doctype html>
<html lang="en">
<head>
<title>Trivia</title>
<link rel='stylesheet' href='css/Trivia.css' >
</head>
<body>
<h1>Trivia</h1>
<div>Score: <span id='score'>Correct 0 Wrong 0</span></div>
<br>
<div>
<label>Select Category:</label>
<select id='category'></select>
<br>
<label>Select Difficulty:</label>
<select id='difficulty'></select>
</div>
<br><br>
<div id='output'></div>
<br>
<div id='selAnswers'></div>
<br>
<button id='btn' type='submit'>Get First Question!</button>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script src ='js/Trivia.js'></script>
</body>
</html>

最佳答案

根据文档,您应该将参数categorydifficultytype 添加到您的GET 请求中,指定从中选择的值您的选择列表。您似乎没有将这些参数添加到您的 ajax 调用中。

除此之外,“category”参数不能作为文本类别(看起来像您要尝试做的)传递,而是作为数字索引类别列表传递。直接来自 API 帮助页面,如下所示:

<option value="9">General Knowledge</option>
<option value="10">Entertainment: Books</option>
<option value="11">Entertainment: Film</option>
<option value="12">Entertainment: Music</option>
<option value="13">Entertainment: Musicals &amp; Theatres</option>
<option value="14">Entertainment: Television</option>
<option value="15">Entertainment: Video Games</option>
<option value="16">Entertainment: Board Games</option>
<option value="17">Science &amp; Nature</option>
<option value="18">Science: Computers</option>
<option value="19">Science: Mathematics</option>
<option value="20">Mythology</option>...

看起来很容易实现。您的代码只是进行基本调用,没有任何参数。只需将它们添加到查询字符串中就可以了。

关于javascript - 根据下拉选择检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65638152/

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