首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery-步骤:如果您之前跳过一步,则使用end循环返回stucks。

Jquery-步骤:如果您之前跳过一步,则使用end循环返回stucks。
EN

Stack Overflow用户
提问于 2014-06-24 07:29:26
回答 1查看 3.4K关注 0票数 3

我正在实现jquery-步骤前进形式

这里我需要跳过一个步骤,如果年龄小于18岁,这是一个类似的例子,在上面的链接中工作,当我试图实现相同的时候,它可以跳过一个步骤,但一旦我需要返回按一下前面的按钮,JS使用被卡住结束少循环!

这里我创建了同样的jsfiddle

这是JS

代码语言:javascript
复制
$(function () {

    $("#form-3").steps({
        bodyTag: "fieldset",
        headerTag: "h1",
        onStepChanging: function (event, currentIndex, newIndex) {
            if (currentIndex > newIndex) {
                return true;
            }

            if (newIndex === 3 && Number($("#age").val()) < 18) {
                return false;
            }

            var form = $(this);

            if (currentIndex < newIndex) {
                $("#form-3 .body:eq(" + newIndex + ") label.error", form).remove();
                $("#form-3 .body:eq(" + newIndex + ") .error", form).removeClass("error");
            }

            return true;
        },
        onStepChanged: function (event, currentIndex, priorIndex) {
            if (currentIndex === 2 && Number($("#age").val()) >= 18) {
                $(this).steps("next");
            }

            if (currentIndex === 2 && priorIndex === 3) {
                $(this).steps("previous");
            }
        },
        onFinishing: function (event, currentIndex) {
            return true;
        },
        onFinished: function (event, currentIndex) {
            var form = $(this);
            form.submit();
        }
    })
});

这是HTML

代码语言:javascript
复制
<form id="form-3" action="#">
        <h1 class="noDisplay">Account</h1>
        <fieldset>
            <legend>Account Information</legend>
            <h3> Your Information</h3>
            <ul>
                <li>
                    <div><label for="userName">User name *</label></div>
                    <div><input id="userName" name="userName" type="text" class="required" data-msg-required="Username Required!"/></div>
                    <div class="cell"><div class="registerError"></div></div>
                </li>
                <li>
                    <div><label for="password">Password *</label></div>
                    <div><input id="password" name="password" type="text" class="required" data-msg-required="Password Required!"></div>
                    <div class="cell"><div class="registerError"></div></div>
                </li>
                <li>
                    <div><label for="confirm">Confirm Password *</label></div>
                    <div><input id="confirm" name="confirm" type="text" class="required" data-msg-required="Confirm Password Required!"></div>
                    <div class="cell"><div class="registerError"></div></div>
                </li>   
            </ul>
            <p>(*) Mandatory</p>
        </fieldset>

        <h1 class="noDisplay">Profile</h1>
        <fieldset>
            <legend>Profile Information</legend>
            <h3> Your Information</h3>
            <label for="name">First name *</label>
            <input id="name" name="name" type="text" class="required">
            <label for="surname">Last name *</label>
            <input id="surname" name="surname" type="text" class="required">
            <label for="email">Email *</label>
            <input id="email" name="email" type="text" class="required email">
            <label for="address">Address</label>
            <input id="address" name="address" type="text">
            <label for="age">Age (The warning step will show up if age is less than 18) *</label>
            <input id="age" name="age" type="text" class="required number">
            <p>(*) Mandatory</p>
        </fieldset>

        <h1 class="noDisplay">Warning</h1>
        <fieldset>
            <legend>You are to young</legend>
            <h3> Your Information</h3>
            <p>Please go away ;-)</p>
        </fieldset>

        <h1 class="noDisplay">Finish</h1>
        <fieldset>
            <legend>Terms and Conditions</legend>
            <h3> Your Information</h3>
            <input id="acceptTerms" name="acceptTerms" type="checkbox" class="required"> <label for="acceptTerms">I agree with the Terms and Conditions.</label>
        </fieldset>
    </form>

我观察到currentIndex和newIndex参数没有得到正确的值,但我不明白为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-25 07:36:19

在onStepChanged中,有两个语句的顺序不对,所以我修改了它,直到现在,它还能正常工作。

我已经在博客文章中修正过了。不过,你自己试试吧,这里

这是固定的JS代码

代码语言:javascript
复制
onStepChanged: function (event, currentIndex, priorIndex)
{
    // Suppress (skip) "Warning" step if the user is old enough and wants to the previous step.
    if (currentIndex === 2 && priorIndex === 3)
    {
        $(this).steps("previous");
        return;
    }

    // Suppress (skip) "Warning" step if the user is old enough.
    if (currentIndex === 2 && Number($("#age").val()) >= 18)
    {
        $(this).steps("next");
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24380783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档