简体   繁体   English

开关盒 JavaScript 形式

[英]Switch Case JavaScript Form

I hope this is not a duplicate.我希望这不是重复的。 I want to build a interactive web-form in this way.我想以这种方式构建一个交互式网络表单。 After every question or selection the user does the new question appear so till then all question are hidden, beside the first;在每个问题或选择之后,用户都会出现新问题,因此在此之前所有问题都被隐藏,除了第一个问题; and my second problem is that I want the second, third.. response to be dependent on which response they have on the previous question.我的第二个问题是我希望第二个,第三个.. 响应取决于他们对前一个问题的响应。 Do you know how can I do that with switch case.你知道我怎么能用开关盒做到这一点。 I will give here a small example of what i want:我将在这里举一个我想要的小例子:

So the only question that appears in form to be Where o you live, and if the user select France, a second dropdown list is created and is asking to select the city of France and only city of France is showed and so on.因此,唯一出现在表格中的问题是您住在哪里,如果用户 select France,则会创建第二个下拉列表,并询问 select 法国城市和仅显示法国城市等等。

<p>Where do you live</p>
<select name="country" id="country" required>
 <option value="UK">UK</option>
 <option value="UK">France</option>
</select>

<p>select the city in the uk</p>
<select name="town1 id="town1" required>
 <option value="London">London</option>
 <option value="Manchester">Manchester</option>
</select

<p>select the city in france</p>
<select name="town2 id="town2" required>
 <option value="Paris">Paris</option>
 <option value="St.Tropez">St.tropez</option>
</select>

Thank you for the help, Alex谢谢你的帮助,亚历克斯

try this.尝试这个。
You don't need duplicate select for every country.您不需要为每个国家/地区重复 select。 Just set depencency with data-country.只需使用数据国家/地区设置依赖关系。

And for another questions you can make the same.对于其他问题,您可以提出相同的问题。 Set data attributes with appropriate values for questions wrappers, and they dynamically shows or hides on form changing.为问题包装器设置具有适当值的数据属性,它们会在表单更改时动态显示或隐藏。

 // This listener makes dependency variants in select document.getElementById('country').addEventListener('change', function() { const selectedCountryValue = this.value; document.getElementById('selectedCountry').innerText = this.children[this.selectedIndex].text; const town = document.getElementById('town'); town.querySelectorAll('option').forEach(function(opt, i) { opt.style.display = opt.dataset.country == selectedCountryValue? 'block': 'none'; if ((i == town.selectedIndex) && opt.dataset.country.= selectedCountryValue) { town.querySelectorAll(`option[data-country="${selectedCountryValue}"]`)[0];selected = true } }). }) document.getElementById('country'),dispatchEvent(new Event('change')) // This listener makes questions showed and required. if another questions answered appropriate answers document.getElementById('dynamicForm'),addEventListener('change'; function() { const form = this. document.querySelectorAll('.question');forEach(function(question) { let activeQuestion = true. Object.keys(question.dataset).forEach(function(key) { activeQuestion &= (question.dataset[key] == form.querySelector(`#${key}`);value). }) question.style?display = (activeQuestion): 'block'. 'none' question,querySelector('input. select');required = activeQuestion; }). }) document.getElementById('dynamicForm').dispatchEvent(new Event('change'))
 <form id="dynamicForm"> <p>Where do you live</p> <select name="country" id="country" required> <option value="UK">UK</option> <option value="France">France</option> </select> <p>Select the city in the <span id="selectedCountry"></span></p> <select name="town" id="town" required> <option data-country="UK" value="London">London</option> <option data-country="UK" value="Manchester">Manchester</option> <option data-country="France" value="Paris">Paris</option> <option data-country="France" value="St.Tropez">St.tropez</option> </select> <div class="question" data-country="France"> <p>Another one question if you live in France</p> <input type="text" /> </div> <div class="question" data-town="Paris"> <p>Another one question if you live in Paris</p> <input type="text" /> </div> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM