gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:04:01 25 4
gpt4 key购买 nike

我有一个多步骤表单,想利用 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"); } );

});

最佳答案

您可以使用 bootstrap 的内置 accordion 并添加一点 javascript 来控制图标:

这是一个 bootply 示例: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");
});

关于javascript - 带有 jquery 切换字段集的多步表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28347505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com