【问题标题】:Jquery - multi-step form progress bar using unordered list items to show completed stepsJquery - 使用无序列表项显示已完成步骤的多步骤表单进度条
【发布时间】:2014-10-08 19:58:05
【问题描述】:

我正在尝试使用 JQuery 构建一个多步骤表单,但我的进度条出现了一些问题。

我的进度条是一个无序列表,其中的列表项应该在用户按下下一步时以红色突出显示。但是,当他们按上一个时,列表项应该会失去突出显示的红色。我非常接近解决方案。我有前两个选项,但是当我进入最后一步时,我没有看到任何突出显示。

这是我的小提琴:http://jsfiddle.net/ktLcfzhe/

JQuery: $(document).ready(function () {

    var current_fs = $('.current'); //current fieldset   

    $('.next').click(function () {
        $('.current').removeClass('current').hide().next().show().addClass('current');


        if ($(this).parent('.field1')) {
            $('#progressbar li').next('.second').addClass('active');
        } else if ($(this).parent('.field2')) {
            $('#progressbar li').next('.last').addClass('active');
        } else if ($(this).parent('.field3')) {
            $('#progressbar li').addClass('active');
        }

    });

    $('.previous').click(function () {
        $('.current').removeClass('current').hide().prev().show().addClass('current');

        if ($(this).parent('.field3')) {
            $('#progressbar li').prev('.last').removeClass('active');
        } else if ($(this).parent('.field2')) {
            $('#progressbar li').prev('.second').removeClass('active');
        }


    });

});

HTML:

<form id="helpdeskform" action="process.php" method="post">
    <!-- Progress Bar -->
    <ul id="progressbar">
        <li class="active first">Identify Yourself</li>
        <li class="second">Describe Request</li>
        <li class="last">Confirm and Submit</li>
    </ul>
    <fieldset class="field1 current">
            <h2>Identify Yourself</h2>

        <p>
            <label for="fname">
                <input type="text" value="" name="" id="" placeholder="First Name" />
            </label>
        </p>
        <p>
            <label for="lname">
                <input type="text" value="" name="" id="" placeholder="Last Name" />
            </label>
        </p>
        <p>
            <label for="Next">
                <input type="button" name="next" class="next action-button" value="Next" />
            </label>
        </p>
    </fieldset>
    <fieldset class="field2">
            <h2>Describe Request</h2>

        <p>
            <label for="">
                <input type="text" value="" name="" id="" placeholder="Subject" />
            </label>
        </p>
        <p>
            <label for="">
                <textarea style="font-family: Arial, Veranda, Sans serif" name="" id="" cols="30" rows="10"></textarea>
            </label>
        </p>
        <p style="float:left;">
            <label for="previous">
                <input type="button" name="previous" class="previous action-button" value="Previous" />
            </label>
        </p>
        <p style="float:left; padding-left: 10px;">
            <label for="Next">
                <input type="button" name="next" class="next action-button" value="Next" />
            </label>
        </p>
    </fieldset>
    <fieldset class="field3">
            <h2>Confirm and Submit</h2>

        <p>
            <label for="fname">
                <input type="text" value="" name="" id="" placeholder="" />
            </label>
        </p>
        <p>
            <label for="">
                <input type="text" value="" name="" id="" placeholder="" />
            </label>
        </p>
        <p>
            <label for="">
                <input type="text" value="" name="" id="" placeholder="" />
            </label>
        </p>
        <p>
            <label for="">
                <input type="text" value="" name="" id="" placeholder="" />
            </label>
        </p>
        <p style="float:left;">
            <label for="previous">
                <input type="button" name="previous" class="previous action-button" value="Previous" />
            </label>
        </p>
        <p style="float:left; padding-left: 10px;">
            <label for="Submit">
                <input type="button" value="Submit" name="submit" id="submit" />
            </label>
        </p>
    </fieldset>
</form>

CSS:

/*form styles*/
 #helpdeskform {
    text-align: center;
    position: relative;
}
/*Hide all except first fieldset*/
 #helpdeskform .field2, .field3 {
    display: none;
}
/*inputs*/
 #helpdeskform input, #helpdeskform textarea {
    padding: 15px;
    border: 1px solid #ccc;
    border-radius: 3px;
    margin-bottom: 10px;
    width: 100%;
    box-sizing: border-box;
    color: #2C3E50;
    font-size: 13px;
}
/*buttons*/
 #helpdeskform .action-button {
    width: 100px;
    font-weight: bold;
    border: 0 none;
    border-radius: 1px;
    cursor: pointer;
    padding: 10px 5px;
    margin: 10px 5px;
}
#helpdeskform .action-button:hover, #msform .action-button:focus {
    box-shadow: 0 0 0 2px white, 0 0 0 3px #900;
}
/*progressbar*/
 #progressbar {
    margin-bottom: 30px;
    overflow: hidden;
    counter-reset: step;
    /*CSS counters to number the steps*/
}
#progressbar li {
    list-style-type: none;
    text-transform: uppercase;
    font-size: 10px;
    width: 30%;
    float: left;
    position: relative;
}
#progressbar li:before {
    content: counter(step);
    counter-increment: step;
    width: 20px;
    line-height: 20px;
    display: block;
    font-size: 10px;
    color: #333;
    background: white;
    border-radius: 3px;
    margin: 0 auto 5px auto;
}
/*progressbar connectors*/
 #progressbar li:after {
    content:'';
    width: 100%;
    height: 2px;
    background: white;
    position: absolute;
    left: -50%;
    top: 9px;
    z-index: -1;
    /*put it behind the numbers*/
}
#progressbar li:first-child:after {
    content: none;
    /*connector not needed before the first step*/
}
/*marking active/completed steps nhlbi red*/

/*The number of the step and the connector before it = red*/
 #progressbar li.active:before, #progressbar li.active:after {
    background: #900;
    color: white;
}

感谢所有 cmets。我使用http://thecodeplayer.com/walkthrough/jquery-multi-step-form-with-progress-bar 作为此表单的指南。 JQuery 对我来说太复杂了。

【问题讨论】:

  • 似乎过于复杂。我不会跟踪谁拥有或没有“.active”类,而是将其从列表中全局删除,然后在当前需要的地方添加类()。
  • 所以在玩 JSFiddle 时,看起来当我单击第一页上的“下一步”并进入“描述您的请求”时,所有三个栏都填满了。
  • 事实上,我今天早上才这样做。看起来非常相似。 $('li', $progressMeter).removeClass('active done'); $('#step2', $progressMeter).addClass('active').prevAll().addClass('done')

标签: javascript jquery html css forms


【解决方案1】:

这是一个工作示例http://jsfiddle.net/ktLcfzhe/1/

jQuery

$('.next').click(function () {
    $('.current').removeClass('current').hide().next().show().addClass('current');
    $('#progressbar li.active').next().addClass('active');

});

$('.previous').click(function () {
    $('.current').removeClass('current').hide().prev().show().addClass('current');
    $('#progressbar li.active').removeClass('active').prev().addClass('active');
});

【讨论】:

  • Bojan 非常感谢您的回答。完美运行。
【解决方案2】:

这是您的解决方案:http://jsfiddle.net/csdtesting/ktLcfzhe/2/

$(document).ready(function () {
    $('.next').click(function () {
        $('.current').removeClass('current').hide().next().show().addClass('current');
        $('#progressbar li.active').next().addClass('active');
        if ($('#progress')) {};

    });

    $('.previous').click(function () {
        $('.current').removeClass('current').hide().prev().show().addClass('current');
        $('#progressbar li.active').removeClass('active').prev().addClass('active');
    });
});

【讨论】:

    【解决方案3】:

    我会通过在更高级别添加一个描述用户所在步骤的类来处理它。

    从#progressbar 上的“第一步”类开始。当用户单击下一步时,添加一个名为“step-two”的类(不要删除“step-one”)。当他们再次点击下一步时,添加第三个类“step-three”。

    然后在你的 CSS 中,使用如下规则:

    #progressbar.step-one .first {
      //show me highlighted
    }
    #progressbar.step-two .second {
      //show me highlighted
    }
    #progressbar.step-three .third {
      //show me highlighted
    }
    

    如果您为下一个按钮提供一个 ID,该 ID 是您希望应用于进度条的类,则您可以在一个单击事件中处理所有这些。

    例如如果第一个下一个按钮看起来像这样:

    <label for="Next">
      <input id="go-to-step-two" type="button" name="next" class="next action-button" value="Next" />
    </label>
    

    那么你的 JavaScript 可能看起来像这样:

    $('.next').click(function(e) {
        var currentPage = this.attr('id').replace('go-to-', ''); //e.g. 'step-two'
        $('#progressbar').class(currentPage);
    });
    

    对后退按钮使用相同的逻辑,只是根据后退按钮的 id 删除类。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多