带有jquery切换字段集的多步表单字段

时间:2015-02-05 15:14:20

标签: javascript jquery html twitter-bootstrap

我有一个多步骤表单,并希望利用jquery和bootstrap来实现以下功能:

  • 当用户点击图例时,其相应的字段集会展开
  • 当用户点击“下一步”按钮时,该字段集将关闭,下一个字段集将打开
  • 当用户点击“上一个”按钮时,该字段集将关闭,之前的字段集会打开

所有这些行为都应切换glyphicon-plus-sign glyphicon-minus-sign图标。我的jquery代码在html下面。

HTML:

 <fieldset>
 <legend class="bg-info"><span class="badge progress-bar-info" style="font-size:18px;">1</span> Registration Details <span class="glyphicon glyphicon-minus-sign pull-right" aria-hidden="true"></span></legend>
    <div class="form_container">Content</div>
    <input type="button" name="next" class="next action-button" value="Next" />
 </fieldset>

<fieldset>
        <legend class="bg-info"><span class="badge progress-bar-info" style="font-size:18px;">2</span> Age Groups and Divisions <span class="glyphicon glyphicon-plus-sign pull-right" aria-hidden="true"></span></legend>
    <div class="form_container" style="display:none;">Content</div>
    <input type="button" name="previous" class="previous action-button" value="Previous" />
    <input type="button" name="next" class="next action-button" value="Next" />
</fieldset>

JQUERY:

$(function(){
    // Set cursor to pointer and add click function
    $("legend").css("cursor","pointer").click(function(){
        var legend = $(this);
       $(this).siblings().slideToggle("slow", function() { legend.children(".glyphicon").toggleClass("glyphicon-plus-sign glyphicon-minus-sign"); } );
    });
});

$(".next").click(function(){

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

    $(this).closest(".form_container").slideToggle("slow", function() { current_fs.closest("legend span.glyphicon").toggleClass("glyphicon-plus-sign glyphicon-minus-sign"); } );
    $(this).next(".form_container").slideToggle("slow", function() { $(this).parent().children("legend.glyphicon").toggleClass("glyphicon-plus-sign glyphicon-minus-sign"); } );

});

1 个答案:

答案 0 :(得分:2)

您可以使用内置accordion内置的bootstrap并添加一些javascript来控制图标来执行此操作:

这是一个很好的例子:http://www.bootply.com/WSrtmlu4W3

HTML

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Registration Details <span class="glyphicon glyphicon-minus"></span>
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
      <div class="panel-body">
        <p>blah blah blah</p>
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo" class="btn btn-default">Next</a> 
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Age Groups and Divisions <span class="glyphicon glyphicon-plus"></span>
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        <p>blah blah blah</p>
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne" class="btn btn-default">Previous</a>
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree" class="btn btn-default">Next</a>
      </div>
    </div>
  </div>
</div>

JS

$('.collapse').on('shown.bs.collapse', function(){
    $(this).parent().find(".glyphicon-plus").removeClass("glyphicon-plus").addClass("glyphicon-minus");
});

$('.collapse').on('hidden.bs.collapse', function(){
    $(this).parent().find(".glyphicon-minus").removeClass("glyphicon-minus").addClass("glyphicon-plus");
});