【问题标题】:How to write tabbed form in html如何在html中编写选项卡式表单
【发布时间】:2023-03-19 01:39:01
【问题描述】:

关于如何在 html 中创建选项卡式表单,请参阅 this example:该示例按预期工作,直到您将代码嵌入到 <form> 元素中。然后单击选项卡会导致表单提交,在示例的情况下,会出现错误。

有人知道解决办法吗?

【问题讨论】:

  • 你能分享一些你尝试过的代码吗?
  • 点击上面的链接,然后点击“自己试试”。尝试单击右侧的选项卡。然后编辑代码,使整个“表单”在
    内,然后重试。
  • 点击选项卡式菜单内的按钮:

    ...
    ...
  • 您看到了什么错误或行为?

标签: html forms tabbed


【解决方案1】:

在您提交的示例部分中,有一个名为 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> 

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多