提交时使用javascript打开表单中的选项卡
我有一个非常简单的表单示例,它被拆分为多个选项卡,然后发布到表单本身,以便使用php进行验证和处理。代码的简化版本如下,没有验证和处理步骤:提交时使用javascript打开表单中的选项卡,javascript,html,forms,Javascript,Html,Forms,我有一个非常简单的表单示例,它被拆分为多个选项卡,然后发布到表单本身,以便使用php进行验证和处理。代码的简化版本如下,没有验证和处理步骤: <style> /* Style the list */ ul.tab { list-style-type: none; margin: 0; padding: 0; overflow: hidden; border: 1px solid #ccc; background-color: #f1f1
<style>
/* Style the list */
ul.tab {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Float the list items side by side */
ul.tab li {float: left;}
/* Style the links inside the list items */
ul.tab li a {
color: black;
display: inline-block;
font-size: 15px;
font-weight: lighter;
padding: 14px 16px;
text-align: center;
text-decoration: none;
transition: all 0.3s ease 0s;
}
/* Change background color of links on hover */
ul.tab li a:hover {background-color: #ddd;}
/* Create an active/current tablink class */
ul.tab li a:focus, .active {background-color: #ccc;}
/* Style the tab content */
.tabcontent {
display: none;
padding: 60px 10px;
border: 1px solid #ccc;
border-top: none;
margin-bottom: 20px;
}
</style>
<ul class="tab">
<li><a href="#" class="tablinks" onclick="openTab(event, 'tab_1')">tab 1</a></li>
<li><a href="#" class="tablinks" onclick="openTab(event, 'tab_2')">tab 2</a></li>
</ul>
<!-- FORM Starts -->
<form method="POST" action="">
<div id="tab_1" class="tabcontent">
Tab One Content
</div>
<div id="tab_2" class="tabcontent">
Tab Two Content
</div>
<input type="submit" name="save_settings_button" value="Save Settings" />
</form>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tabcontent.length; i++) {
tablinks[i].classList.remove("active");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.classList.add("active");
}
var mybtn = document.getElementsByClassName("tablinks")[0];
mybtn.click();
</script>
这有助于将大表单分解为多个部分。但是,如果用户在选项卡2上单击“提交”按钮,它们将返回到选项卡1,这不是一个很好的用户体验
是否有任何方法可以使用javascript在提交表单时识别哪个选项卡处于焦点位置,然后在页面重新加载后在该选项卡上重新加载页面?我会检查是否存在活动类,以确定哪个选项卡当前处于活动状态。e、 比如tablink.classList.contains'active' 由于您在提交表单后将使用php处理所有内容,因此在页面刷新后了解活动选项卡的唯一方法是在表单中添加自定义字段,并在更改选项卡时进行更新 所以在你的html中
<input type="hidden" id="active_tab" name="active_tab" value="tab_1" />
现在在php中,当加载页面时,添加一个新变量来捕获活动选项卡,并将活动类更新到选项卡中
$active_tab = $_POST['active_tab'];
$active_tab1 = "";
$active_tab2 = "";
if ($active_tab ==="tab_1") $active_tab1 = "active";
if ($active_tab ==="tab_2") $active_tab2 = "active";
然后最后更新选项卡以使用活动类
<div id="tab_1" class="tabcontent <?php echo $active_tab1; ?>">...</div>
div id="tab_2" class="tabcontent <?php echo $active_tab2; ?>">...</div>
但这并不是最佳方式,为了获得更好的用户体验,我会将所有表单提交更改为ajax调用,并在外部php文件中处理所有内容
<div id="tab_1" class="tabcontent <?php echo $active_tab1; ?>">...</div>
div id="tab_2" class="tabcontent <?php echo $active_tab2; ?>">...</div>