有什么方法可以为数据流生成 javascript 或伪代码吗?
我有一个网络表单,用户可以通过下拉菜单回答问题,我希望根据他们的回答提供其他问题。
例如,如果您的企业有营业场所,请询问营业时间。如果他们不这样做,请显示他们运营的区域。
随着我添加到流程中的每个问题,代码变得越复杂。许多 if/then 函数并不总是具有互斥的答案。
虽然每个用户可能只需回答 5-10 个问题,但可能的问题数量可能100+。
有没有一种简单的方法来生成 Javascript/PHP 或至少生成伪代码,让您可以绘制一条通过所有可用答案的路径,并为您生成相关的嵌套 if/then 语句?
示例:Akinator 根据之前的答案猜测电视角色。
考虑到某些答复,当然,页面不必必须进行编码,以便所有问题都在一页上得到解答,但我想知道这是否至少可能。
提前致谢。我希望你们都喜欢 WWDC ;)
I have a web form where users answer questions via dropdowns, and I wish to make other questions available based on their responses.
e.g. if your business has a premises then ask about opening hours. If they don't, show the regions they operate in.
With each question I add to the process, the more complex the code becomes. A lot of if/then functions that don't always have mutually exclusive answers.
Whilst each user may only have to answer 5-10 questions, the number of possible questions can be 100+.
Is there a simple way to generate either Javascript/PHP or at the very least pseudo code which lets you plot a path through all the available answers and it generates the relevant nested if/then statements for you?
Example: Akinator guesses TV characters based on previous answers.
In anticipation of certain responses, of course the page doesn't have to be coded so that all questions are answered on one page, but I want to know if it's at least possible.
Thanks in advance. I hope you all enjoy WWDC ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在包含所有规则的页面上设置一个 json 对象,并在每个元素上设置一个与 json 对象相对应的 id/属性。然后,在更改时,您检查 json 对象中是否有与“更改”元素的属性相对应的“触发”节点,然后找到应根据该值隐藏/显示的所有元素。请注意,这段代码非常粗糙,不是一个确切的答案,但足以让您入门。
You can set up a json object on the page that contains all of the rules and on each element have an id/attribute that corresponds to the json object. Then on change you check the json object for a "trigger" node that corresponds to the "change" element's attribute and then find all the elements that should be hidden/shown based upon that value. Note this code is really rough and not an exact answer but enough of an idea to get you started.
我不会考虑生成大量代码,而是考虑避免大量代码。
如果您有所有可能问题的列表,并且知道哪些是“开始”问题,那么您需要了解每个问题是:
这样,你只需要一个知道如何提出问题并根据答案采取行动的代码。当它从用户那里获取答案时,它应该将答案累积在列表中。您甚至可以提供导航方案以允许用户向后走。
Instead of thinking in terms of generating a lot of code, I'd think in terms of avoiding a lot of code.
If you've got a list of all possible questions, and you know which ones are the "start" questions, then what you need to know for each of them is:
With that, you just need one piece of code that knows how to present a question and act on the answer. It should accumulate the answers in a list, as it gets them from the user. You can even provide a navigation scheme to allow users to go backwards.