简体   繁体   English

Javascript 显示/隐藏多层次深度

[英]Javascript Show/Hide on multiple levels deep

I need help understanding how to write show/hide javascript where there are multiple levels of elements to show/hide.我需要帮助了解如何编写显示/隐藏 javascript,其中有多个级别的元素要显示/隐藏。

I hope this isn't too difficult to explain.我希望这不太难解释。

How our currents system works, is that a CSS class is added to an input such as a radio button option or checkbox.我们的 currents 系统的工作原理是将 CSS 类添加到输入中,例如单选按钮选项或复选框。 The CSS class would be "show-question2" CSS 类将是“show-question2”

The element that needs to be toggled for "show/hide" is given the class "showable-question2"需要为“显示/隐藏”切换的元素被赋予类“showable-question2”

The Javascript function listens for the click event, matches the css class "show-{0} with the element that has the corresponding "showable-{0}" and toggles its visibility. Javascript 函数侦听单击事件,将 css 类“show-{0}”与具有相应“showable-{0}”的元素匹配并切换其可见性。

MY ISSUE:我的问题:

For example, if you have a question, that depending on how you answer it, will show/hide a new question.例如,如果您有一个问题,根据您的回答方式,将显示/隐藏一个新问题。

But if how you answer that second question means that a third question is "shown", i run into a problem.但是,如果您如何回答第二个问题意味着“显示”了第三个问题,我就会遇到问题。

If i am to go back to answer the original question differently, as expected the second question will be hidden, as its visibility has been toggled OFF, but the third dependent question, is still visible.如果我要回去以不同的方式回答原始问题,正如预期的那样,第二个问题将被隐藏,因为它的可见性已被关闭,但第三个相关问题仍然可见。

In order to achieve my goal, do these elements need to nest in the Html?为了实现我的目标,这些元素是否需要嵌套在 Html 中?

EDIT编辑

The problem is that questions have dependancies on other questions.问题是问题对其他问题有依赖性。

So If Question 1, is answered with an answer from a list, that then triggers a second question, the second question in this instance is shown to the end user.因此,如果问题 1 用列表中的答案回答,然后触发第二个问题,则此实例中的第二个问题将显示给最终用户。

So your toggling Question 2's visibility on how Question 1 is answered.因此,您切换问题 2 的可见性如何回答问题 1。

Question 2 may have dependants also, so if the user answers question 2 with a specific option, a third question will become visible.问题 2 也可能有依赖项,因此如果用户使用特定选项回答问题 2,则第三个问题将变得可见。

Question 3, is a dependant of Question 2.问题 3 是问题 2 的依赖项。

Now, If the user decides that they have changed their mind, and pick a different answer to Question 1, there will be no requirement now for Question 2, and it will be hidden.现在,如果用户决定他们已经改变主意,并为问题 1 选择不同的答案,那么现在对问题 2 没有要求,它将被隐藏。

The problem being, that Question 1, does not toggle the visiblity of Question 3, which will remain visible, and is only relevant to Question 2, which has been hidden and when no longer required.问题是,问题 1 不会切换问题 3 的可见性,问题 3 将保持可见,并且仅与问题 2 相关,该问题已被隐藏且不再需要时。

Why don't you change your javascript to implement the behaviour you are seeking?你为什么不改变你的 javascript 来实现你正在寻求的行为? Basically, you want to implement a sequence of states, with some specific rules about state transition, which you need to put into a function.基本上,您想要实现一系列状态,以及一些关于状态转换的特定规则,您需要将这些规则放入函数中。 Your model is an array [{question_number, answer, answer_eval}] .您的模型是一个数组[{question_number, answer, answer_eval}] answer_eval can be for example good , bad . answer_eval可以是例如goodbad Your state is an array [{question number, show}] .您的状态是一个数组[{question number, show}] show can be for instance display or hide . show可以是例如displayhide

When you click on a question, you get the number of that question, you have the current state and model in your arrays, and you want to decide your next state.当你点击一个问题时,你会得到那个问题的编号,你的数组中有当前的状态和模型,你想决定你的下一个状态。 Write the function that does just that.编写实现该功能的函数。

so it a function f :: (event, state, model) -> (state, model) which could be specified around those lines :所以它是一个函数f :: (event, state, model) -> (state, model)可以在这些行周围指定:

  • event : click事件:点击

    1. get the number of the question获取问题的编号
    2. show that question显示这个问题
    3. hide all other questions with answer_eval NOT good (that should cover hiding your third question)answer_eval隐藏所有其他问题good (应该包括隐藏你的第三个问题)
    4. hide all other dependent questions (that should cover hiding your second question)隐藏所有其他相关问题(应该包括隐藏你的第二个问题)
    5. update model and state arrays更新模型和状态数组
  • event : evaluate answer事件:评估答案

    1. get the number of the question获取问题的编号
    2. compute answer_eval计算answer_eval
    3. good => show dependent question(s)好 => 显示相关问题
    4. bad => your behaviour here不好 =>你在这里的行为
    5. update model and state arrays更新模型和状态数组

I did not understand exactly what was the behaviour you want after a click, but the principle will always be the same.我不明白点击后你想要什么行为,但原理总是一样的。 If you are able to specify it, you are able to write it.如果您能够指定它,您就可以编写它。

Hope that helps希望有帮助

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

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