Jquery多步表格

时间:2014-08-17 01:54:01

标签: javascript jquery html forms

我在网上找到了这条Jquery和HTML代码,创建了一个多步骤表单。但是,我希望能够操作代码,以便用户可以跳转到特定步骤。

例如:在表格的顶部,我提出了一个问题,即“你喜欢数学还是科学”。表单包含两个按钮,一个表示“数学”,另一个表示“科学”。目前,如果单击任一按钮,它将引导您进入表单的下一步。但是,我想这样做,如果用户点击“数学”,它将采用与用户点击“科学”时不同的形式。

以下是代码的外观,您可以看到演示@:http://thecodeplayer.com/walkthrough/jquery-multi-step-form-with-progress-bar

<!-- multistep form -->
<form id="msform">

    <fieldset>
        <h2 class="fs-title"> Do you enjoy Math or Science?</h2>
        <h3 class="fs-subtitle">This is step 1</h3>

        <input type="button" name="next" class="next action-button" value="Math" /> </li>

        <input type="button" name="next" class="next action-button" value="Science" /> </li>

    </fieldset>

    <fieldset>
        <h2 class="fs-title">Math</h2>
        <h3 class="fs-subtitle">Your presence on the social network</h3>
        <input type="text" name="twitter" placeholder="Twitter" />
        <input type="text" name="facebook" placeholder="Facebook" />
        <input type="text" name="gplus" placeholder="Google Plus" />
        <input type="button" name="previous" class="previous action-button" value="Previous" />
        <input type="button" name="next" class="next action-button" value="Next" />
    </fieldset>


    <fieldset>
        <h2 class="fs-title">Science</h2>
        <h3 class="fs-subtitle">We will never sell it</h3>
        <input type="text" name="fname" placeholder="First Name" />
        <input type="text" name="lname" placeholder="Last Name" />
        <input type="text" name="phone" placeholder="Phone" />
        <textarea name="address" placeholder="Address"></textarea>
        <input type="button" name="previous" class="previous action-button" value="Previous" />
        <input type="button" name="next" class="next action-button" value="Next" />
    </fieldset>

    <fieldset>
        <h2 class="fs-title">Personal Details</h2>
        <h3 class="fs-subtitle">We will never sell it</h3>
        <input type="text" name="fname" placeholder="First Name" />
        <input type="text" name="lname" placeholder="Last Name" />
        <input type="text" name="phone" placeholder="Phone" />
        <textarea name="address" placeholder="Address"></textarea>
        <input type="button" name="previous" class="previous action-button" value="Previous" />
        <input type="submit" name="submit" class="submit action-button" value="Submit" />
    </fieldset>

</form>

<script>
//jQuery time
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

$(".next").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show(); 
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50)+"%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({'transform': 'scale('+scale+')'});
            next_fs.css({'left': left, 'opacity': opacity});
        }, 
        duration: 800, 
        complete: function(){
            current_fs.hide();
            animating = false;
        }, 
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".previous").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    previous_fs = $(this).parent().prev();

    //de-activate current step on progressbar
    $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

    //show the previous fieldset
    previous_fs.show(); 
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale previous_fs from 80% to 100%
            scale = 0.8 + (1 - now) * 0.2;
            //2. take current_fs to the right(50%) - from 0%
            left = ((1-now) * 50)+"%";
            //3. increase opacity of previous_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({'left': left});
            previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
        }, 
        duration: 800, 
        complete: function(){
            current_fs.hide();
            animating = false;
        }, 
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".submit").click(function(){
    return false;
})
</script>

<!-- jQuery -->
<script src="http://thecodeplayer.com/uploads/js/jquery-1.9.1.min.js" type="text/javascript"></script>
<!-- jQuery easing plugin -->
<script src="http://thecodeplayer.com/uploads/js/jquery.easing.min.js" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:3)

<强>供参考:

我给出了详细的答案。因此,请完整阅读答案并了解我在代码中所做的所有更改。

详细答案:

根据您的要求为每个fieldset提供标识。如下所示,

<fieldset id="firstField"> <!-- First Step -->
<fieldset id="mathsField"> <!-- Maths -->
<fieldset id="scienceField"> <!-- Science -->

您的js代码基于.next.previous类正在运行。所以,我们应该用我们的逻辑来改变这种流动。您已经为按钮数学和科学分配了名称,如下所示,

<input type="button" name="maths" class="next action-button" value="Math" />
<input type="button" name="science" class="next action-button" value="Science" />

因此,在$(".next").click(function(){ });中,只需通过按钮name检查,如果点击的按钮是数学或科学,并将相应的fieldset DOM设置为变量{{1像下面的代码一样,

next_fs

并在if($(this).attr('name') == 'maths') next_fs = $('#mathsField'); if($(this).attr('name') == 'science') next_fs = $('#scienceField'); $(".previous").click(function(){ }); DOM设置为变量#firstField,如下面的代码所示,

previous_fs

见这个JSFIDDLE DEMO