有什么方法可以为数据流生成 javascript 或伪代码吗?

发布于 2024-11-13 09:31:53 字数 522 浏览 2 评论 0原文

我有一个网络表单,用户可以通过下拉菜单回答问题,我希望根据他们的回答提供其他问题。

例如,如果您的企业有营业场所,请询问营业时间。如果他们不这样做,请显示他们运营的区域。

随着我添加到流程中的每个问题,代码变得越复杂。许多 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-11-20 09:31:53

您可以在包含所有规则的页面上设置一个 json 对象,并在每个元素上设置一个与 json 对象相对应的 id/属性。然后,在更改时,您检查 json 对象中是否有与“更改”元素的属性相对应的“触发”节点,然后找到应根据该值隐藏/显示的所有元素。请注意,这段代码非常粗糙,不是一个确切的答案,但足以让您入门。

<select id='q1' onchange='handleHideShow(this)'></select>
<select id='q2' onchange='handleHideShow(this)'></select>

function handleHideShow(el)
{
    var elValue = el.value;
    if(hsObj[el.id])
    {
       var rules = hsObj[el.id].rules;
       for(var r = 0; r < rules.length; r++)
       {
           var rRule = rules[r];
           for(var t in rRule) 
           { 
               var showEl = document.getElementById(rRule[t]);
               if(showEl)
               {
                    var hideVal = "";
                    if(elValue != t)
                    {
                        hideVal = 'none';
                    }
                    showEl.styles.display = hideVal;
                }
            }
        }
    }               
}
var hsObj = [
   "q1":{"Yes":"q2"}
];

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.

<select id='q1' onchange='handleHideShow(this)'></select>
<select id='q2' onchange='handleHideShow(this)'></select>

function handleHideShow(el)
{
    var elValue = el.value;
    if(hsObj[el.id])
    {
       var rules = hsObj[el.id].rules;
       for(var r = 0; r < rules.length; r++)
       {
           var rRule = rules[r];
           for(var t in rRule) 
           { 
               var showEl = document.getElementById(rRule[t]);
               if(showEl)
               {
                    var hideVal = "";
                    if(elValue != t)
                    {
                        hideVal = 'none';
                    }
                    showEl.styles.display = hideVal;
                }
            }
        }
    }               
}
var hsObj = [
   "q1":{"Yes":"q2"}
];
浅忆 2024-11-20 09:31:53

我不会考虑生成大量代码,而是考虑避免大量代码。

如果您有所有可能问题的列表,并且知道哪些是“开始”问题,那么您需要了解每个问题是:

  • 问题(文本)
  • 问题的“名称”;换句话说,当你积累答案时,簿记的一些关键
  • 可能的答案(“是”/“否”或选项列表)
  • 对于每个可能的答案,下一个要问的问题的身份

这样,你只需要一个知道如何提出问题并根据答案采取行动的代码。当它从用户那里获取答案时,它应该将答案累积在列表中。您甚至可以提供导航方案以允许用户向后走。

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:

  • the question (text)
  • the "name" of the question; in other words, some key for bookkeeping as you accumulate answers
  • possible answers ("yes"/"no" or a list of options)
  • for each possible answer, the identity of the next question to ask

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文