javascript - 如何通过使用 jQuery 单击按钮来获得下一个问题
问题描述
我想修改这个 jQuery 代码,这样当我按下“下一步”按钮时,应该会出现下一个问题,而不是通过单击单选按钮。
<!DOCTYPE html>
<html>
<head>
<title>jQuery Get Selected Radio Button Value</title>
<style>
* {
background-color: wheat;
font-size: 40px;
align-self: center;
}
.big {
width: 20px;
height: 20px;
}
#title {
font-size: 50px;
text-align: center;
width: 100%;
color: tomato
}
div {
width: 100%;
display: block;
text-align: center;
}
#next{
float:right;
margin-right: 20%;
color:tomato
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
$('#game,#movie,#sport,#genre,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#hobbie input').on('change',function () {
var x = $('#game,#movie,#sport,#genre,#action,#game,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport ').hide();
$('#' + $(this).val()).slideDown();
})
$('#movie input').change(function () {
$('#game,#sport,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#' + $(this).val()).show();
})
$('#game input').change(function () {
$('#movie,#sport,#genre,#action,#game#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#' + $(this).val()).show();
})
$('#sport input').change(function () {
$('#game,#movie,#genre,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#' + $(this).val()).show();
})
// for button position
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 1.2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
$('#next').center();
});
</script>
</head>
<body>
<h1 id="title">Interest</h1>
<div class="level1">
<div id="hobbie">
<hr>
<hr>
<hr>
<input type="radio" class="big" name="hobbie" value="movie">movie</input>
<input type="radio" class="big" name="hobbie" value="game">games</input>
<input type="radio" class="big" name="hobbie" value="sport">sports</input>
</div>
</div>
<div class="level2">
<div id="movie">
<hr>
<hr>
<hr>
<input type="radio" class="big" name="movie" value="action">action</input>
<input type="radio" class="big" name="movie" value="adventure">adventure</input>
<input type="radio" class="big" name="movie" value="scifi">Sci-Fi</input>
</div>
<div id="game">
<hr>
<hr>
<hr>
<input type="radio" class="big" name="game" value="gta">GTA</input>
<input type="radio" class="big" name="game" value="fpp">FPP</input>
<input type="radio" class="big" name="game" value="historic">Historic</input>
</div>
<div id="sport">
<hr>
<hr>
<hr>
<input type="radio" class="big" name="sport" value="indoor">indoor</input>
<input type="radio" class="big" name="sport" value="outdoor">outdoor</input>
<input type="radio" class="big" name="sport" value="esport">esport</input>
</div>
</div>
<div class="level3">
<div id="action">
<hr>
<hr>
<input type="radio" class="big" name="action" value="matrix">the Matrix</input>
<input type="radio" class="big" name="action" value="terminator">Terminator</input>
<input type="radio" class="big" name="action" value="madmax">Mad Max</input>
</div>
<div id="adventure">
<hr>
<hr>
<input type="radio" class="big" name="adventure" value="jumanji">Jumanji</input>
<input type="radio" class="big" name="adventure" value="rings">Lord of the Rings</input>
<input type="radio" class="big" name="adventure" value="avatar">Avatar</input>
</div>
<div id="scifi">
<hr>
<hr>
<input type="radio" class="big" name="scifi" value="inception">Inception</input>
<input type="radio" class="big" name="scifi" value="prometheus">Prometheus</input>
<input type="radio" class="big" name="scifi" value="martian">The Martian</input>
</div>
<div id="gta">
<hr>
<hr>
<input type="radio" class="big" name="gta" value="gtav">GTA-V</input>
<input type="radio" class="big" name="gta" value="vice">Vice City</input>
<input type="radio" class="big" name="gta" value="san">San Andreas</input>
</div>
<div id="fpp">
<hr>
<hr>
<input type="radio" class="big" name="fpp" value="battle">Battlefield</input>
<input type="radio" class="big" name="fpp" value="cod">Call of Duty</input>
<input type="radio" class="big" name="fpp" value="pubg">PUBG</input>
</div>
<div id="historic">
<hr>
<hr>
<input type="radio" class="big" name="historic" value="witcher">The Witcher Hunter</input>
<input type="radio" class="big" name="historic" value="assasin">Assasins Creed</input>
<input type="radio" class="big" name="historic" value="dead">Red Dead Redemption</input>
</div>
<div id="indoor">
<hr>
<hr>
<input type="radio" class="big" name="indoor" value="ludo">ludo</input>
<input type="radio" class="big" name="indoor" value="carrom">carrom</input>
<input type="radio" class="big" name="indoor" value="cards">cards</input>
</div>
<div id="outdoor">
<hr>
<hr>
<input type="radio" class="big" name="outdoor" value="cricket">Cricket</input>
<input type="radio" class="big" name="outdoor" value="football">Football</input>
<input type="radio" class="big" name="outdoor" value="golf">Golf</input>
</div>
<div id="esport">
<hr>
<hr>
<input type="radio" class="big" name="esport" value="valorant">Valorant</input>
<input type="radio" class="big" name="esport" value="dota">Dota-2</input>
<input type="radio" class="big" name="esport" value="cs">CS-GO</input>
</div>
</div>
<button id='next'>Next</button>
</body>
</html>
解决方案
我没有得到您要求用户执行多项操作(选择选项并单击按钮)的意思,他只需在代码中选择任何选项即可获得相应的选项。无论如何,您可以尝试下面的代码,通过单击下一步按钮来获得下一个问题。(这可能是硬编码的例子:P)
<script>
$(document).ready(function () {
$('#game,#movie,#sport,#genre,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#hobbie input').on('change',function () {
var x = $('#game,#movie,#sport,#genre,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
})
$('#movie input').change(function () {
$('#game,#sport,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
})
$('#game input').change(function () {
$('#movie,#sport,#genre,#action,#game#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
})
$('#sport input').change(function () {
$('#game,#movie,#genre,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
})
// for button position
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 1.2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
$('#next').center();
$("#next").on('click', function(){
var hobbieselected = $('input[name=hobbie]:checked').val();
var movieselected = $('input[name=movie]:checked').val();
var gameselected = $('input[name=game]:checked').val();
var sportselected = $('input[name=sport]:checked').val();
if(hobbieselected == 'movie'){
$('#game,#sport,#gta,#fpp,#action,#adventure,#scifi#historic,#indoor,#outdoor,#esport').hide();
$('#movie').show();
}
else if(hobbieselected == 'game'){
$('#movie,#sport,#gta,#fpp,#action,#adventure,#scifi#historic,#indoor,#outdoor,#esport').hide();
$('#game').show();
}
else if(hobbieselected == 'sport'){
$('#movie,#game,#gta,#fpp,#action,#adventure,#scifi#historic,#indoor,#outdoor,#esport').hide();
$('#sport').show();
}
if(hobbieselected == 'movie' && movieselected == 'action'){
$('#game,#sport,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#action').show();
}
else if(hobbieselected == 'movie' && movieselected == 'adventure'){
$('#sport,#action,#scifi,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#adventure').show();
}
else if(hobbieselected == 'movie' && movieselected == 'scifi'){
$('#sport,#action,#adventure,#gta,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#scifi').show();
}
if(hobbieselected == 'game' && gameselected == 'gta'){
$('#sport,#adventure,#scifi,#action,#fpp,#historic,#indoor,#outdoor,#esport').hide();
$('#gta').show();
}
else if(hobbieselected == 'game' && gameselected == 'fpp'){
$('#movie,#sport,#genre,#action,#game#adventure,#scifi,#gta,#historic,#indoor,#outdoor,#esport').hide();
$('#fpp').show();
}
else if(hobbieselected == 'game' && gameselected == 'historic'){
$('#movie,#sport,#genre,#action,#game#adventure,#scifi,#gta,#fpp,#indoor,#outdoor,#esport').hide();
$('#historic').show();
}
if(hobbieselected == 'sport' && sportselected == 'indoor'){
$('#game,#adventure,#scifi,#gta,#fpp,#historic,#action,#outdoor,#esport').hide();
$('#indoor').show();
}
else if(hobbieselected == 'sport' && sportselected == 'outdoor'){
$('#game,#movie,#genre,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#esport').hide();
$('#outdoor').show();
}
else if(hobbieselected == 'sport' && sportselected == 'esport'){
$('#game,#movie,#genre,#action,#adventure,#scifi,#gta,#fpp,#historic,#indoor,#outdoor').hide();
$('#esport').show();
}
});
});
</script>
推荐阅读
- javascript - 为什么 eth.contract.Transfer 只返回选择的交易?
- android - 无法从 Windows 2016 服务器获得对特定图像文件的任何响应
- wordpress - 排除 Woocommerce 中的类别
- python-3.x - 我需要为提到的页面中的每个职位抓取职位描述文本
- c++ - 如何从输入的文本文件中按姓氏字母顺序对图表进行排序?
- node.js - 如何在对话流中获取先前的意图
- google-apps-script - 更新其他工作表时更新谷歌“主”电子表格
- jpa - 使用 micronaut-data-hibernate-jpa 库连接到 jpa 时出错
- reactjs - 使用 Firebase 在两个应用之间共享位置
- c# - 使用 Master.Master 上的 javascript 在任何 aspx 页面上从 Master.cs 调用方法