html - 如何在html中编写选项卡式表单
问题描述
参考这个关于如何在 html 中创建选项卡式表单的示例:该示例按预期工作,直到您将代码嵌入到<form>
元素中。然后单击选项卡会导致表单提交,在示例的情况下,会出现错误。
有谁知道摆脱这种情况的方法?
解决方案
在您提交的示例部分中,有一个名为 OpenCity 的函数用于传递事件。要阻止它刷新或提交,您只需阻止默认状态:
<script>
function openCity(evt, cityName) {
evt.preventDefault();
...
}
</script>
function openCity(evt, cityName) {
evt.preventDefault();
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 < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
body {font-family: Arial;}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
.button {
padding:15px;
border: none 0px white;
border-color: white;
background-color: #318DE8;
border-radius: 3px;
color: white;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>
<form>
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
<input name="value_1" value="" placeholder="Insert Value that will remain when switched"/>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
<input name="value_2" value="" placeholder="Insert Value that will remain when switched"/>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
<input name="value_3" value="" placeholder="Insert Value that will remain when switched"/>
</div>
<br>
<input class="button" type="submit" value="Submit Form" />
</form>
</body>
</html>
推荐阅读
- node.js - 在我的个人项目上安装 Node-Saas 错误
- python - 如何访问对象填充字典中的值?
- python - 重新定位当前使用 Tkinter 网格定位的棋盘游戏
- git - 有没有什么方法可以让多个设备(人)在数据块中同时在不同的分支中工作?
- r - 在 Rmarkdown 中打印树时出现奇怪的字符
- postgresql - 使用 AWS/RDS `pg_read_server_files role to COPY`运行 IHP 作业
- c# - 仅当我将 Console.WriteLine 放入 Getter 时才会触发事件
- c - 将 8 位有符号整数转换为无符号整数,然后再转换为 int32
- php - Laravel 数据库连接问题
- sql - 动态查询 - 每周更新 - Oracle SQL