私は非常に単純なクイズを持っています。クイックで一度に1つの要素を表示する方法
一度に1つの質問のみを表示するにはどうすればよいですか? '次へ'と '前へ'ボタンを追加する。私はまだJavascriptの初心者ですが、私はこれに対する答えを見つけることができません。
<html>
<head>
<meta charset="UTF-8">
<title>What kind of car should I buy?</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
</head>
<body>
<h1>What kind of car should I buy?</h1>
<p id="suggestion"></p>
<form id="form" action="">
<p>What size do you need?</p>
<input type="radio" name="size" value="compact">Compact<br>
<input type="radio" name="size" value="mid">Midsize<br>
<input type="radio" name="size" value="small">Small SUV<br>
<input type="radio" name="size" value="med">Medium SUV<br>
<input type="radio" name="size" value="big">Big SUV
<p>What kind of driving will you be doing?</p>
<input type="radio" name="drive" value="city">City/suburb driving<br>
<input type="radio" name="drive" value="mountain">Mountain driving
<p>Which would you rather have?</p>
<input type="radio" name="mpg" value="gas">Good gas milage<br>
<input type="radio" name="mpg" value="performance">Performance
<p>How much do luxury features matter to you?</p>
<input type="radio" name="money" value="save">I'd rather save money and have less luxury features<br>
<input type="radio" name="money" value="luxury">I'd rather spend a little more and have luxury features.
<p><input id="submit" type="submit" value="submit"></p>
</form>
</body>
</html>
そして、これはそれを管理するためのいくつかの方法があり、私のJavascript
const types = {
size: {
compact: 'compact',
mid: 'mid',
small: 'small',
med: 'med',
big: 'big'
},
drive: {
city: 'city',
mountain: 'mountain'
},
mpg: {
gas: 'gas',
performance: 'performance'
},
money: {
save: 'save',
luxury: 'luxury'
}
};
const cars = [
{
car: 'Prius',
requirements: {
size: types.size.compact,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'BMW i-3',
requirements: {
size: types.size.compact,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Civic SI',
requirements: {
size: types.size.compact,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'BMW 328',
requirements: {
size: types.size.compact,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Mini Cooper All4',
requirements: {
size: types.size.compact,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'BMW 328 XI',
requirements: {
size: types.size.compact,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'BMW XI',
requirements: {
size: types.size.compact,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Subaru Impreza',
requirements: {
size: types.size.compact,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'Accord Hybrid',
requirements: {
size: types.size.mid,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'BMW 528',
requirements: {
size: types.size.mid,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'VW CC',
requirements: {
size: types.size.mid,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'Subaru Legacy',
requirements: {
size: types.size.mid,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'BMW 528 XI',
requirements: {
size: types.size.mid,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Dodge Charger AWD',
requirements: {
size: types.size.mid,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'Audi A4 Quattro',
requirements: {
size: types.size.mid,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Rav4 Hybrid',
requirements: {
size: types.size.small,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'Buick Encore',
requirements: {
size: types.size.small,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Mazda CX-5',
requirements: {
size: types.size.small,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'BMW X3',
requirements: {
size: types.size.smal,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Honda CR-V AWD',
requirements: {
size: types.size.small,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'Buick Encore AWD',
requirements: {
size: types.size.small,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Mazda CX-5 AWD',
requirements: {
size: types.size.small,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'BMW X3 AWD',
requirements: {
size: types.size.small,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Highlander Hybrid',
requirements: {
size: types.size.med,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'Buick Enclave',
requirements: {
size: types.size.med,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Mazda CX-7',
requirements: {
size: types.size.med,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'BMW X5',
requirements: {
size: types.size.med,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Honda Pilot AWD',
requirements: {
size: types.size.med,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'Buick Enclave AWD',
requirements: {
size: types.size.med,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Mazda CX-7 AWD',
requirements: {
size: types.size.med,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.money
}
},
{
car: 'BMW X5 AWD',
requirements: {
size: types.size.med,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Chevy Tahoe',
requirements: {
size: types.size.big,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'Audi A7',
requirements: {
size: types.size.big,
drive: types.drive.city,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Chevy Tahoe',
requirements: {
size: types.size.big,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'Cadallac Escalade',
requirements: {
size: types.size.big,
drive: types.drive.city,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
{
car: 'Chevy Tahoe 4WD',
requirements: {
size: types.size.big,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.save
}
},
{
car: 'Infiniti QX80 4WD',
requirements: {
size: types.size.big,
drive: types.drive.mountain,
mpg: types.mpg.gas,
money: types.money.luxury
}
},
{
car: 'Ford Expedition 4WD',
requirements: {
size: types.size.big,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.save
}
},
{
car: 'Caddalac Escalade 4WD',
requirements: {
size: types.size.big,
drive: types.drive.mountain,
mpg: types.mpg.performance,
money: types.money.luxury
}
},
];
const getElements =() => {
const checkedValue = selector => (
document.querySelector('input[name = "' + selector + '"]:checked').value
);
return {
size: checkedValue('size'),
drive: checkedValue('drive'),
mpg: checkedValue('mpg'),
money: checkedValue('money'),
submit: document.getElementById("submit"),
suggestion: document.getElementById("suggestion")
};
}
function submit(event) {
event.preventDefault();
const {
size,
drive,
mpg,
money,
suggestion
} = getElements();
const result = cars.find(({
requirements
}) => (
requirements.size === size &&
requirements.drive === drive &&
requirements.mpg === mpg &&
requirements.money === money
));
suggestion.innerHTML = result.car;
}
document.getElementById("form").addEventListener("submit", submit);