好吧,所以我遇到了一个小问题,我有两个jQuery按钮“previous”和“Next”,现在我需要做的是让第一个问题上的前一个按钮不可见因为你不能倒退下一个按钮在最后一页上不可见,因为它是最后一页。什么是最好的方法来解决这个问题。这是我的代码.....
if (i.Question_Type == "DROPDOWN")
{
<div class="container text-center">
<div class="row idrow" data-questions="@counter">
@{counter++;
}
<div id="question1" class="form-group">
<label class="lab text-center" for="form-group-select">
@i.Question_Order @Html.Raw(@i.Question)
</label>
<select class="form-control" id="form-group-select">
@for (int x = 1; x <= Convert.ToInt32(i.Question_SubType); x++)
{
var t = x - 1;
if (i.qOps != null)
{
<option> @i.qOps.options[t]</option>
}
else
{
<option> @x</option>
}
}
</select>
</div>
</div>
</div>
}
if (i.Question_Type == "RADIO")
{
<div class="container">
<div class="row idrow" data-questions="@counter">
@{counter++;
}
<div class="form-group">
<label class="lab" for="questions">
@i.Question_Order @i.Question
</label>
<div class="row">
<div class="col-xs-12">
<div id="question1" class="radio-inline">
@for (int x = 1; x <= Convert.ToInt32(i.Question_SubType); x++)
{
var t = x - 1;
if (i.qOps != null)
{
<label class="radio-inline"><input type="radio" name="question"> @i.qOps.options[t]</label>
}
else
{
<label class="radio-inline"><input type="radio" min="0" max="@x" name="question"></label>
}
}
</div>
</div>
</div>
</div>
</div>
</div>
}
if (i.Question_Type == "CHECKBOX")
{
for (int y = 1; y <= Convert.ToInt32(i.Question_SubType); y++)
{
@*<div class="container">
<div class="row">
<label>@y</label> <input type="checkbox" name="question">
</div>
</div>*@
}
}
}
<div class="azibsButtons">
<button type="button" id="previous" class="btn btn-primary pull-left">Prev</button>
<button type="button" id="next" class="btn btn-primary pull-right">Next</button>
</div>
<script>
$(document).ready(function () {
$(".idrow").each(function (i) {
var inner = $(this).data('questions');
if (inner == 0) {
$(this).removeClass('hidden');
} else {
$(this).addClass('hidden');
}
});
$("#next").click(function () {
$(".idrow").each(function (i) {
var inp = $(this);
if (!inp.hasClass('hidden')) {
var dataVal = inp.data("questions");
dataVal++;
inp.addClass('hidden');
$('[data-questions=' + dataVal + ']').removeClass('hidden');
return false;
}
});
});
$("#previous").click(function () {
$(".idrow").each(function (i) {
var inp = $(this);
if (!inp.hasClass('hidden')) {
var dataVal = inp.data("questions");
dataVal--;
inp.addClass('hidden');
$('[data-questions=' + dataVal + ']').removeClass('hidden');
return false;
}
});
});
});
</script>
答案 0 :(得分:0)
您的代码应该是这样的。试试这个,如果您需要任何解释请告诉我
$(document).ready(function () {
ShowTheelement(0); //run a function
$("#previous").addClass('hidden'); // hide the previous button
var dataVal = 0; // set dataVal to 0
$("#next").click(function () {
dataVal++; // add 1 to data val on each click
$("#previous").removeClass('hidden');// show the previous button
dataVal == $(".idrow[data-questions]").length - 1 ? $(this).addClass('hidden'): $(this).removeClass('hidden'); // similar to if/else statement .. If dataVal equal the numbers of `idrow` - 1 hide the next button if not show the next button
ShowTheelement(dataVal); // run a function to hide divs and show just the one we need with new `dataVal`
});
$("#previous").click(function () {
dataVal--;
$("#next").removeClass('hidden');
dataVal == 0 ? $(this).addClass('hidden'): $(this).removeClass('hidden');
ShowTheelement(dataVal);
});
});
// function to hide all `.idrow` elements and show the one with `data-questions='"+dataVal+"'`
function ShowTheelement(dataVal){
$(".idrow").addClass('hidden');
$(".idrow[data-questions='"+dataVal+"']").removeClass('hidden');
}
解释:
当您需要添加/删除类onload时,单击下一个按钮并单击上一个按钮。无需每次都重复您的代码。最好为此创建一个函数并将dataVal
传递给此函数
有关详细说明,请阅读代码