短表单与切换标签

huangapple go评论81阅读模式
英文:

short form with switching tabs

问题

我只会提供代码的翻译部分:

我只是尝试编写一个简单的表单,用于在选项卡之间切换。目前我只在进行功能部分,没有进行数据收集,但我遇到了一个问题。我试图让下一个按钮跳转到下一个选项卡,似乎可以工作,但然后立即切回到第一个选项卡。有点困惑。以下是我目前的代码。有什么想法?

// 你的HTML和JavaScript代码

请注意,我只提供了代码的翻译,没有包括问题的回答。

英文:

I'm just trying to write a simple form that switches between tabs. I'm just doing the functionality part without the data collecting right now, but I hit a snag. I'm trying to have the next button go to the next tab, and it seems to work, but then it switches right back to the first tab immediately. At a bit of a loss. Below is what I have atm. Any thoughts?

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<style>

.tab {display: none }
#submit {display: none}

</style>
<body>

    <form>
        <div class="tab">
          <input type="text" placeholder="Full Name" id="fname" name="fname"><br><br>
              <input type="text" placeholder="Email" id="femail" name="femail">
        </div>
          <div class="tab">
          <input type="text" placeholder="Date of Birth" id="fdob" name="fdob"><br><br>
              <input type="text" placeholder="Phone Number" id="fnumber" name="fnumber">
        </div>
          <div class="tab">
          <input type="text" placeholder="Address" id="faddress" name="faddress"><br><br>
              <input type="text" placeholder="ZIP Code" id="fzip" name="fzip">
        </div><br><br>
      <button id="previous">Previous</button><button id="next" onclick="nextTab(currentTab)">Next</button><br><br>
      <input type="button" id ="submit" value="Submit">
        
      </form>

<script>

let currentTab = 0;
let x = document.getElementsByClassName("tab");

x[currentTab].style.display="inline";

if (currentTab == 0) {
  document.getElementById("previous").style.display = "none";
};

function nextTab(f) {
  
  x[f].style.display = "none";
  
  let a = currentTab += 1; 
  
  x[a].style.display = "inline";

  currentTab = a;
} 

</script>

</body>
</html>

I tried to set the currentTab variable in the nextTab function to, but it still goes right to the first tab. I'm probably missing a detail that's stopping me from getting this to work.

答案1

得分: 1

你的主要问题是你没有将*type="button"*添加到按钮中,因为按钮默认为type="submit",这会在每次单击“下一步”或“上一步”按钮时提交表单。

我最初发现这一点时,我已经重新编写了大部分JS以检查其中的错误,因此这里对JS部分采取了略微不同的方式,但不是必要的 - 最重要的是将按钮设置为type="button"

英文:

Your main problem is that you did not add type="button" to your buttons, since buttons default to be type="submit" this submits your form each time you click a Next or Previous button.

I first discovered this when I had already rewritten much of the JS to check for errors in it, so here a slighlty different take on the JS part, but not necessary - most important is set your buttons to type="button":

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;link href=&quot;https://fonts.googleapis.com/css?family=Raleway&quot; rel=&quot;stylesheet&quot;&gt;
&lt;style&gt;
  .tab {
    display: none
  }

  #submit {
    display: none
  }
&lt;/style&gt;

&lt;body&gt;

  &lt;form&gt;
    &lt;div class=&quot;tab&quot;&gt;
      &lt;input type=&quot;text&quot; placeholder=&quot;Full Name&quot; id=&quot;fname&quot; name=&quot;fname&quot;&gt;&lt;br&gt;&lt;br&gt;
      &lt;input type=&quot;text&quot; placeholder=&quot;Email&quot; id=&quot;femail&quot; name=&quot;femail&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;tab&quot;&gt;
      &lt;input type=&quot;text&quot; placeholder=&quot;Date of Birth&quot; id=&quot;fdob&quot; name=&quot;fdob&quot;&gt;&lt;br&gt;&lt;br&gt;
      &lt;input type=&quot;text&quot; placeholder=&quot;Phone Number&quot; id=&quot;fnumber&quot; name=&quot;fnumber&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;tab&quot;&gt;
      &lt;input type=&quot;text&quot; placeholder=&quot;Address&quot; id=&quot;faddress&quot; name=&quot;faddress&quot;&gt;&lt;br&gt;&lt;br&gt;
      &lt;input type=&quot;text&quot; placeholder=&quot;ZIP Code&quot; id=&quot;fzip&quot; name=&quot;fzip&quot;&gt;
    &lt;/div&gt;&lt;br&gt;&lt;br&gt;
    &lt;button type=&quot;button&quot; id=&quot;previous&quot;&gt;Previous&lt;/button&gt;
    &lt;button type=&quot;button&quot; id=&quot;next&quot;&gt;Next&lt;/button&gt;&lt;br&gt;&lt;br&gt;
    &lt;input type=&quot;button&quot; id=&quot;submit&quot; value=&quot;Submit&quot;&gt;

  &lt;/form&gt;

  &lt;script&gt;
    let currentTab = 0;
    document.querySelector(&#39;.tab:nth-child(1)&#39;).style.display = &#39;inline&#39;;

    document.body.addEventListener(&#39;click&#39;, e =&gt; {
      // check if click on next or previous btn, if not return
      let nextBtn = e.target.closest(&#39;#next&#39;);
      let prevBtn = e.target.closest(&#39;#previous&#39;);
      if (!nextBtn &amp;&amp; !prevBtn) { return; }
      // get all tabs and hide them
      let tabs = [...document.querySelectorAll(&#39;.tab&#39;)];
      tabs.forEach(tab =&gt; tab.style.display = &#39;&#39;);
      // calculate current tab
      currentTab += (nextBtn &amp;&amp; 1) + (prevBtn &amp;&amp; -1);
      if (currentTab &gt;= tabs.length) { currentTab = 0; }
      if (currentTab &lt; 0) { currentTab = tabs.length - 1; }
      // show current tab
      tabs[currentTab].style.display = &#39;inline&#39;;
    });

  &lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月7日 04:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定