【问题标题】:HTML5 Tab including form and submit buttonHTML5 选项卡,包括表单和提交按钮
【发布时间】:2021-12-22 02:48:21
【问题描述】:

我创建了一个网页,您可以在其中切换到伦敦、巴黎、东京的标签。 每个选项卡都有一个名为评论 1、评论 2 和评论 3 的文本字段。 Tab Tokyo 还有一个提交按钮,一旦按下它,输入文本字段 cmets 1,2,3 应该被发送到服务器 (broker4)。 但是,在打开网页并按下“伦敦”选项卡后,网页立即开始发送到服务器,然后我才输入文本字段评论 1。

你能看看下面的代码吗?

最好的问候,

科内利斯

<!DOCTYPE html>
  <html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
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;
}
</style>
</head>


<body>
<FORM ACTION="/scripts/broker4.exe" METHOD="FLAG" ENCTYPE="multipart/form-data" language="JavaScript" name="FrontPage_Form1" style="width: 844px" > 


<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>

<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>Enter text here.</p>
  <p><input type="text" name="comment1" value=""></p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Enter text here.</p> 
  <p><input type="text" name="comment2" value=""></p>
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Enter text here.</p>
  <p><input type="text" name="comment3" value=""></p>
  <p><input type="button" name="Send" value="Submit" onClick="location.href='http://nlgosas/scripts/broker4.exe?comment=&_SERVICE=emmerich&_PROGRAM=gvm.aaa.sas'"></p>
</div>

<script>
function openCity(evt, cityName) {
  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";
}
</script>
 
<INPUT TYPE="HIDDEN" NAME="_SERVICE" SIZE="-1" VALUE="emmerich"><INPUT TYPE="HIDDEN" NAME="_PROGRAM" SIZE="-1" VALUE="gvm.aaa.sas">
</body>
</html> 



【问题讨论】:

  • 你所有的 &lt;button class="tablinks" 都是 submit 按钮,并且你在你的 JS 中没有做任何事情来阻止点击这些按钮的默认操作(提交表单。)添加 type="button"让它们只是点击按钮。
  • 确实,你说的很对,这很有帮助。我没有意识到默认选项。

标签: javascript html css forms tabs


【解决方案1】:

默认情况下,所有&lt;button&gt;s 都将提交他们所在的任何表单。要阻止这种情况,请将它们的类型设置为 "button":

<button class="tablinks" type="button" onclick="openCity(event, 'London')">London</button>

【讨论】:

  • 非常感谢您的宝贵意见,这真的很有帮助,此外,其他 javascripts 工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2013-01-21
相关资源
最近更新 更多