Acordeón Jquery, asistente siguiente y anterior, ¿cómo obtener las secciones anteriores y siguientes?
-
07-07-2019 - |
Pregunta
Tengo un acordeón Jquery que funciona bien. Las secciones se expanden / colapsan cuando se hace clic en los encabezados respectivos. Pero quiero agregar la funcionalidad para que cuando haga clic en "siguiente" abriendo la siguiente sección y haciendo clic en "Anterior" El botón me lleva de vuelta a la sección anterior.
Esto se ha hecho en esta página http: //jquery.bassistance .de / accordion / demo /? p = 1.1.2 (último ejemplo) pero no estoy seguro de cómo implementarlo en mi caso.
Cualquier sugerencia por favor.
Gracias
ACTUALIZACIÓN: ¿Cómo obtengo la sección anterior o siguiente del acordeón?
<script type="text/javascript">
$("#accordion").accordion({
header: "h3.header"
, autoHeight: false
, collapsible: true
});
</script>
<div id="accordion">
<h3 class="header">Section 1</h3>
<div> content 1 .. content 1 .. content 1 .. content 1 ..
<input class="next" type="button" value="next"/>
</div>
<h3 class="header">Section 2</h3>
<div> content 2 .. content 2 .. content 2 .. content 2 ..
<input class="previous" type="button" value="previous"/>
<input class="next" type="button" value="next"/>
</div>
<h3 class="header">Section 3</h3>
<div> content 3 .. content 3 .. content 3 .. content 3 ..
<input class="previous" type="button" value="previous"/>
</div>
</div>
Solución
Pude hacerlo funcionar. Prueba esto, funciona con jQuery 1.6 y jQuery UI 1.8:
HTML:
<div id="checkout">
<h2><a href="#">Section 1</a></h2>
<div>
<form action="#">
<h3>Content</h3>
<input type="submit" class="next" value="Continue" />
</form>
</div>
<h2><a href="#">Section 2</a></h2>
<div>
<form action="#">
<h3>Content</h3>
<input type="submit" class="prev" value="Back" />
</form>
</div>
</div>
jQuery:
<script type="text/javascript">
$(document).ready(function () {
// create new accordion
var checkout = $('#checkout').accordion({ event: false });
// assign accordion navigation events to button clicks
$('form', checkout).each(function (index) {
$(this)
.children(':submit')
.filter('.next, .prev')
.click(function () {
checkout.accordion('option', 'active', index + ($(this).is('.next') ? 1 : -1));
return false;
});
});
});
</script>
Otros consejos
Agregue esto a sus archivos jQuery. Agrega una función $ (" # accordion "). Accordion (" next ") y una opción, " loopOnNext " (valor predeterminado falso) para forzarlo a volver al cuadro superior cuando esté al final.
$.extend($.ui.accordion.prototype, {
next: function(){
var index = this.option('active') || 0,
next = index + 1,
nodes = $(this.element).children('h3').length;
if(next >= nodes && this.option('loopOnNext') === true) {
next = 0;
}
return this.activate(next);
}
});
Tuve un problema similar y usé:
<button onclick="$('#accordion').accordion('activate', 0);">Prev</button>
<button onclick="$('#accordion').accordion('activate', 2);">Next</button>
en cada panel de acordeón. Esto está en el segundo panel y lo lleva de regreso al primero (índice de 0) o al tercero (índice de 2).
¿Has intentado hacer lo mismo que en el ejemplo?
var wizard = $("#accordion").accordion({
header: 'h3.header',
event: false
});
$("h3.header", wizard).each(function(index) {
$(this)
.next()
.children(":button")
.filter(".next, .previous")
.click(function() {
wizard.changeAccordion("activate", index + ($(this).is(".next") ? 1 : -1))
});
});
Estaba buscando una solución al mismo problema y se me ocurrió esto (estoy trabajando con jQuery 1.4.2 y jQuery UI 1.8.1):
<div id="wizard">
<h3><a href="#step1">Step 1 / 3</a></h3>
<div id="step1">
<button type="button">Next »</button>
</div>
<h3><a href="#step2">Step 2 / 3</a></h3>
<div id="step2">
<button type="button">Next »</button>
</div>
<h3><a href="#step3">Step 3 / 3</a></h3>
<div id="step3">
<button type="submit">Finish</button>
</div>
</div>
Y este es el guión:
<script type="text/javascript">
$(function() {
$('#wizard').accordion();
$('#wizard :button').each(function (index, elm) {
$(elm).click(function() {
$('#wizard').accordion('activate', index + 1);
});
});
});
</script>
prueba esto:
var wizard = $("#register_form").accordion({
header: '.title',
event: 'none',
collapsible: false
});
$(':button.previous').click(function() { wizard.accordion("activate", 0); });
$(':button.next').click(function() { wizard.accordion("activate", 1); });
Donde : button
son los botones anterior y siguiente, y wizard
es su acordeón jQuery.
Deja fuera el resto del código. A ver si esto funciona.
Por favor, deje comentarios, gracias.