我是一名学生,也是PHP的新手。我目前正在开发一个技能测试项目,在每个选项卡上显示一个问题,我包括应该响应/自动的上一个和下一个按钮。例如,如果我在选项卡2中,则前一个按钮应该指向选项卡1,下一个按钮指向选项卡3.如果我转到选项卡3,则前面的btn应该指向选项卡2,下一个指向选项卡4,因此在..下一个按钮工作得很好,但我不知道为什么以前的按钮不会工作。我希望有人可以帮助我。请。非常感谢你!
这是我的代码:
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
document.getElementById("defaultOpen").click();
</script>
<div class="tab">
<button class="btn btn-default btnPrevious" data-direction="previous" type="button" onclick=""><span class="glyphicon glyphicon-triangle-left"></span></button>
<button class="tablinks" type="button" onclick="openTab(event, 'q1')" id="defaultOpen">1</button>
<button class="tablinks" type="button" onclick="openTab(event, 'q2')">2</button>
<button class="tablinks" type="button" onclick="openTab(event, 'q3')">3</button>
<button class="tablinks" type="button" onclick="openTab(event, 'q4')">4</button>
<button class="btn btn-default btnNext" data-direction="next" type="button" onclick=""><span class="glyphicon glyphicon-triangle-right"></span></button>
</div>
<form action="results.php" method="POST">
<?php if (mysqli_num_rows($result) > 0): ?>
<?php $index = 1; ?>
<?php foreach ($result as $results):?>
<div id="q<?php echo ($index++); ?>" class="tabcontent">
<table class="table table-hover">
<tbody>
<tr class="form-group">
<h3 name="ques[<?php echo $test_id;?>]" style="text-indent: 40px;"> <?php echo $results['question']; ?> </h3>
</tr>
<tr class="form-group">
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiona'];?>"><?php echo $results['optiona'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionb'];?>"><?php echo $results['optionb'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionc'];?>"><?php echo $results['optiond'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
<input style="font-size: 18px;" type="radio" ame="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiond'];?>"><?php echo $results['optiond'];?>
</label>
<br>
</tr>
</tbody>
</table>
<center>
<button class="btn btn-default btnBack" data-direction="back" type="button" onclick="openTab(event, 'q<?php echo ($index-=1); ?>')"><span class="glyphicon glyphicon-triangle-left"></span> Back</button>
<button class="btn btn-default btnNext" data-direction="next" type="button" onclick="openTab(event, 'q<?php echo ($index+=1); ?>')"><span class="glyphicon glyphicon-triangle-right"></span> Next</button>
</center>
</div>
<?php endforeach ?>
<?php endif ?>
答案 0 :(得分:1)
检查这是否适合你
$('.btn-right').click(function(){
$('.nav-tabs > .active').next('li').find('a').trigger('click');
});
$('.btn-left').click(function(){
$('.nav-tabs > .active').prev('li').find('a').trigger('click');
});
&#13;
a.btn-left,a.btn-right{
display:inline-block;
padding:5px 10px;
font-size:24px;
cursor:pointer;
}
.tab_desc {
min-height: 150px;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#home">Home</a></li>
<li><a data-toggle="tab" href="#menu1">About</a></li>
<li><a data-toggle="tab" href="#menu2">Cases</a></li>
<li><a data-toggle="tab" href="#menu3">Experience</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
<div class="tab_desc">
<h3>HOME</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div class="pull-right pagination">
<a class="btn-right" id="btnNext"><i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<div id="menu1" class="tab-pane fade">
<div class="tab_desc">
<h3>Menu 1</h3>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class="pull-right pagination">
<a class="btn-left"><i class="fa fa-arrow-circle-left"></i></a>
<a class="btn-right"><i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<div id="menu2" class="tab-pane fade">
<div class="tab_desc">
<h3>Menu 2</h3>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
</div>
<div class="pull-right pagination">
<a class="btn-left"><i class="fa fa-arrow-circle-left"></i></a>
<a class="btn-right"><i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<div id="menu3" class="tab-pane fade">
<div class="tab_desc">
<h3>Menu 3</h3>
<p>Eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
</div>
<div class="pull-right pagination">
<a class="btn-left"><i class="fa fa-arrow-circle-left"></i></a>
</div>
</div>
</div>
</div>
&#13;