简体   繁体   English

如何制作一个多步表单,其中选项基于先前的响应?

[英]How to make a multistep form where options are based on previous response?

I have recently began working with JavaScript.我最近开始使用 JavaScript。 I was wondering how I could make a multi-step form that gives options based on the previous steps replies.我想知道如何制作一个多步骤表单,根据前面的步骤回复提供选项。 More specifically I am trying to make a game and in the first step the number of players is entered and in the second step I want text boxes for player names based on the number of players specified.更具体地说,我正在尝试制作游戏,第一步输入玩家人数,第二步我想要基于指定玩家人数的玩家姓名文本框。 How would I do this?我该怎么做? I was thinking I could use innerHTML?我在想我可以使用innerHTML? What would be best to use for this?为此最好使用什么? Any help is appreciated.任何帮助表示赞赏。

It sounds as though you have a long learning curve ahead of you, so I'd recommend using something like Vue.js (which is still Javascript based) to abstract out your coding requirements.听起来好像您的学习曲线很长,所以我建议使用Vue.js (仍然基于 Javascript)之类的东西来抽象出您的编码要求。 Your question then becomes quite simple: https://codepen.io/MSCAU/pen/QWOLbBN?editors=1111你的问题就变得很简单: https://codepen.io/MSCAU/pen/QWOLbBN?editors=1111

Of course, you'd need to learn Vue, but you might find it an easier path than hand-coding fairly granular functions in JS.当然,您需要学习 Vue,但您可能会发现它比在 JS 中手动编写相当精细的函数更容易。 It depends if you're doing this as a project to learn Javascript fully - in which case don't go down this road - or to produce a game which works within a reasonable amount of time.这取决于您是否将其作为一个项目来完全学习 Javascript - 在这种情况下不要 go 沿着这条路走 - 或者制作一个在合理时间内工作的游戏。

JS JS

const app = new Vue({
  el: '#app',
  data: {
    num_players: 1,
    player_names: []
  }
});

HTML HTML

<div id="app">
  <h3>Number of players</h3>
  <input type="number" v-model.number="num_players"/>
  <hr/>
  <h3>Player names</h3>
  <table>
    <tr v-for="n in num_players">
      <td>Player {{n}}</td>
      <td><input type="text" v-model="player_names[n-1]"/></td>
    </tr>
  </table>
  <hr/>
  <h3>Output</h3>
  {{ player_names }}
</div>

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

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