3

我正在实施jquery-Step Advance Form

在这里,如果年龄小于 18 岁,我需要跳过一个步骤,这是上面链接中的一个类似示例,当我尝试实现相同时,跳过一个步骤很好,但是一旦我需要返回单击上一个按钮, JS 使用 end less 循环来卡住!!

在这里,我为相同的内容创建了 jsfiddle

这里是 JS

$(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

<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 参数没有得到正确的值,但我不明白为什么?

4

1 回答 1

5

在 onStepChanged 中有两个语句的顺序错误,所以我改变了它,现在它工作正常。

我已经在博客文章中修复了它。但是在这里自己尝试一下。

这是固定的JS代码

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");
    }
}
于 2014-06-25T07:36:19.597 回答