大家好,我正在创建自定义多步骤表单但是当我点击下一步按钮时,它无法触发相关的< a>锚标签(自动点击)
请帮我解决我的问题
提前致谢
$(function() {
$("#nextBtn").click(function() {
var $tabs = $("li");
$tabs.each(function() {
var activeTab = $(this).hasClass("active");
if (activeTab == true) {
//var activeAnch = $(this).children().attr("href");
alert($(this).next('li').children().attr("href"));
var nextId = $(this).next('li').children().attr("id");
//$("#"+nextId).click();// trigger click to #menu 1 <a>
}
});
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Case</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<ul class="nav nav-pills">
<li class="active"><a data-toggle="pill" href="#home">Home</a></li>
<li><a data-toggle="pill" href="#menu1">Menu 1</a></li>
<li><a data-toggle="pill" href="#menu2">Menu 2</a></li>
<li><a data-toggle="pill" href="#menu3">Menu 3</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
<h3>HOME</h3>
<span>Form 1</span>
</div>
<div id="menu1" class="tab-pane fade">
<h3>Menu 1</h3>
<span>Form 2</span>
</div>
<div id="menu2" class="tab-pane fade">
<h3>Menu 2</h3>
<span>Form 3</span>
</div>
<div id="menu3" class="tab-pane fade">
<h3>Menu 3</h3>
<span>Form 4</span>
</div>
</div>
<button id="nextBtn">Next</button>
</body>
</html>
解决方法:
我对Parvez Rahman的回答做了一些改变.您无需通过javascript在元素内部单击.我想这就像反模式.
您可以使用引导功能显示带有选项卡功能的下一个选项卡,而不是单击该元素.
$(function() {
$("#nextBtn").click(function() {
$('li.active').next().find('a').tab("show"); //show tab of the next li
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Case</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<ul class="nav nav-pills">
<li class="active"><a data-toggle="pill" href="#home">Home</a></li>
<li><a data-toggle="pill" href="#menu1">Menu 1</a></li>
<li><a data-toggle="pill" href="#menu2">Menu 2</a></li>
<li><a data-toggle="pill" href="#menu3">Menu 3</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
<h3>HOME</h3>
<span>Form 1</span>
</div>
<div id="menu1" class="tab-pane fade">
<h3>Menu 1</h3>
<span>Form 2</span>
</div>
<div id="menu2" class="tab-pane fade">
<h3>Menu 2</h3>
<span>Form 3</span>
</div>
<div id="menu3" class="tab-pane fade">
<h3>Menu 3</h3>
<span>Form 4</span>
</div>
</div>
<button id="nextBtn">Next</button>
</body>
</html>