javascript 如何将输入值结转到多步骤表单中的下一个步骤?

gopyfrb3  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(165)

我想将#firstname-input的值向前推进,并在#firstname中显示。如何使用JS?
我是JS的新手,尝试了多种解决方案,但没有一个工作。
请帮助我的JS代码,将结转的第一步输入的名字的值Step1到Step2跨度。

const prevBtn = document.querySelectorAll(".prev-btn");
const nextBtn = document.querySelectorAll(".next-btn");
let currentSection = 0;

function showSection(sectionIndex) {
  const sections = document.querySelectorAll(".steps");
  sections[currentSection].classList.remove("active-step");
  sections[sectionIndex].classList.add("active-step");
  currentSection = sectionIndex;
}

function nextSection() {
  if (currentSection < 2) {
    showSection(currentSection + 1);
  }
}

function prevSection() {
  if (currentSection > 0) {
    showSection(currentSection - 1);
  }
}
.steps{
    display: none;
}

.active-step{
    display: block;
}
<form action="">
    <!-- Step1 -->
    <div class="steps active-step" id="step1">
        <div class="step-title">What's your first name?</div>
        <div class="step-content">
            <div class="input-area">
                <input type="text" class="text-input" placeholder="First Name" id="firstname-input">
            </div>
        </div>
        <div class="btn-next-box">
            <button onclick="nextSection()" class="next-btn" id="step1-next">Next</button>
        </div>
    </div>

    <!-- Step2 -->
    <div class="steps" id="step">
        <button onclick="prevSection()" class="prev-btn">Prev</button>
        <div class="step-title"><span id="firstname"></span>,which club you're in?</div>
        <div class="step-content">
            <div class="input-area">
                <input type="text" class="text-input" placeholder="your club name" id="club-input">
            </div>
        </div>
        <div class="btn-next-box">
            <button onclick="nextSection()" class="next-btn" id="step1-next"></button>
        </div>
    </div>
</form>
11dmarpk

11dmarpk1#

在JavaScript中,可以使用<input><textarea>元素的.value property读取该元素的值。要设置元素的文本内容,可以使用.innerText property
此外,当<form>中有一个<button>元素时,您可能需要确保type属性设置为button(例如:<button>)。<button type="button">...</button>),这是因为默认类型是submit,它将触发表单的提交。(更多信息:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
下面的代码包括我上面提到的修复,通过监听输入的change event,这允许同步从输入到<span>元素的值:

const prevBtn = document.querySelectorAll(".prev-btn");
const nextBtn = document.querySelectorAll(".next-btn");
let currentSection = 0;

const firstNameInput = document.getElementById('firstname-input');
const firstNameSpans = Array.from(document.querySelectorAll('.firstname'));
firstNameInput.addEventListener('change', () => {
  firstNameSpans.forEach((firstNameSpan) => { firstNameSpan.innerText = firstNameInput.value; });
});

function showSection(sectionIndex) {
  const sections = document.querySelectorAll(".steps");
  sections[currentSection].classList.remove("active-step");
  sections[sectionIndex].classList.add("active-step");
  currentSection = sectionIndex;
}

function nextSection() {
  if (currentSection < 2) {
    showSection(currentSection + 1);
  }
}

function prevSection() {
  if (currentSection > 0) {
    showSection(currentSection - 1);
  }
}
.steps{
    display: none;
}

.active-step{
    display: block;
}
<form action="">
    <!-- Step1 -->
    <div class="steps active-step" id="step1">
        <div class="step-title">What's your first name?</div>
        <div class="step-content">
            <div class="input-area">
                <input type="text" class="text-input" placeholder="First Name" id="firstname-input">
            </div>
        </div>
        <div class="btn-next-box">
            <button onclick="nextSection()" type="button" class="next-btn" id="step1-next">Next</button>
        </div>
    </div>

    <!-- Step2 -->
    <div class="steps" id="step">
        <button onclick="prevSection()" type="button" class="prev-btn">Prev</button>
        <div class="step-title"><span class="firstname"></span>,which club you're in?</div>
        <div class="step-content">
            <div class="input-area">
                <input type="text" class="text-input" placeholder="your club name" id="club-input">
            </div>
        </div>
        <div class="btn-next-box">
            <button onclick="nextSection()" type="button" class="next-btn" id="step1-next"></button>
        </div>
    </div>
</form>

相关问题