gpt4 book ai didi

javascript - 单击按钮获取下一个/上一个 ID

转载 作者:行者123 更新时间:2023-11-28 12:34:19 26 4
gpt4 key购买 nike

我有这个 HTML:

<form action="" id="product_create" method="post">
<ul>
<li id="tab1" data-display="categories-picker"><a href="#">Seleccionar categoría</a></li>
<li id="tab2" data-display="product-create-step-2"><a href="#">Datos</a></li>
<li id="tab3" data-display="product-create-step-3" style="display: none"><a href="#">Variaciones</a></li>
<li id="tab4" data-display="product-create-step-4"><a href="#">Detalles del Producto</a></li>
<li id="tab5" data-display="product-create-step-5"><a href="#">Condiciones de Venta</a></li>
</ul>

<fieldset id="categories-picker">
....
</fieldset>

<fieldset id="product-create-step-2" style="display: none">
....
</fieldset>

<fieldset id="product-create-step-3" style="display: none">
....
</fieldset>

<fieldset id="product-create-step-4" style="display: none">
....
</fieldset>

<fieldset id="product-create-step-5" style="display: none">
....
</fieldset>

<button type="button" name="finish-wizard" id="finish-wizard" style="display:none">Finish</button>
</form>

<button type="button" name="previous" id="previous-step" disabled="disabled">&#171; Previous</button>
<button type="button" name="next" id="next-step">Next &#187;</button>

为了切换或隐藏上一个/显示下一个,当我单击 $(input[name="next"])$(input[name="previous"]) 但我编写了以下代码:

var first = $('fieldset').eq(0).attr('id');
var step = 1;

$('#next-step').click(function() {
if (step >= 1) {
$("#previous-step").removeAttr("disabled");
}

var current = $('fieldset').eq(step).next('fieldset');
var previous = $('fieldset').eq(step).prev('fieldset');
current.show();
previous.hide();
step++;
});

$('#previous-step').click(function() {
var current = $('fieldset').eq(step).next('fieldset');
var previous = $('fieldset').eq(step).prev('fieldset');
current.hide();
previous.show();
step--;
});

但它工作不正常,有人可以帮我吗?

最佳答案

这可能更简单,使用 .prev() 而不是 .next() 复制功能来获取上一个按钮的代码。

$('fieldset.step').hide().eq(0).show();

$('#next-step').click(function() {

var current = $('fieldset.step:visible'),
next = current.next('fieldset.step');
if (next.length === 0) {
next = current.nextUntil('fieldset.step').next('fieldset.step');
}
current.hide();
next.show();
if (next.nextUntil('fieldset.step').next('fieldset.step').add(next.next('fieldset.step')).length === 0) {
$("#next-step").prop("disabled",true);
}
$("#previous-step").prop("disabled",false);
});

$('#previous-step').click(function() {

var current = $('fieldset.step:visible'),
prev = current.prev('fieldset.step');
if (prev.length === 0) {
prev = current.prevUntil('fieldset.step').prev('fieldset.step');
}
current.hide();
prev.show();
if (prev.prevUntil('fieldset.step').prev('fieldset.step').add(prev.prev('fieldset.step')).length === 0) {
$("#previous-step").prop("disabled",true);
}
$("#next-step").prop("disabled",false);
});

不要忘记提供需要通过 .step 类循环的字段集。不再需要 id。

Fiddle

关于javascript - 单击按钮获取下一个/上一个 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18725554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com