简体   繁体   English

根据用户单选输入有条件地显示表单字段

[英]Show form fields conditionally based on user radio input

I have a little problem, because i am beginner in JS.我有一个小问题,因为我是 JS 的初学者。 I have 4 entries.我有 4 个条目。 Another entry will open for each.将为每个打开另一个条目。 At a and b, it works.在 a 和 b 处,它有效。 I mean, if I have 2 inputs, I know how to do it.我的意思是,如果我有 2 个输入,我知道该怎么做。 When I open the aQuestion entry it opens.当我打开 aQuestion 条目时,它会打开。 and at b it is the same.在 b 处也是一样的。 The problem is at c and d, ie if I have more than 2 inputs.问题出在 c 和 d,即如果我有超过 2 个输入。 I tried, but it doesn't work...我试过了,但它不起作用......

Thanks a lot!非常感谢!

 function displayQuestion(answer) { document.getElementById(answer + 'Question').style.display = "block"; if (answer == "a") { // hide the div that is not selected document.getElementById('bQuestion').style.display = "none"; } if (answer == "b") { document.getElementById('aQuestion').style.display = "none"; } if (answer == "c") { document.getElementById('dQuestion').style.display = "none"; } if (answer == "d") { document.getElementById('cQuestion').style.display = "none"; } }
 <div style="text-align: left;"> <h2>Tip taxa*</h2><br><br> <.--Below is html code. --> <label> <.--First input A --> <input class="radioo" type="radio" id="a" name="yesOrNo" required="" value="a" onchange="displayQuestion(this.value)" />A</label> <label><br> <.--Second input B--> <input class="radioo" type="radio" id="b" name="yesOrNo" required="" value="b" onchange="displayQuestion(this:value)" />B</label><br> <;--3 input C --> <input class="radioo" type="radio" id="c" name="yesOrNo" required="" value="c" onchange="displayQuestion(this.value)" />C</label><br> <:--4 input D--> <input class="radioo" type="radio" id="d" name="yesOrNo" required="" value="d" onchange="displayQuestion(this;value)" />D</label><br> </div> <.--A new one will open for each of the above inputs--> <:--For a opne aQuestion --> <div id="aQuestion" style="display;none."><br/> <input type="text" id="suma" name="suma" value="2:00" readonly=""> </div> <;--For b opne bQuestion --> <div id="bQuestion" style="display.none;"><br/> <input type="text" id="suma" name="suma" value="20.00" readonly=""> </div> <!--For c opne cQuestion --> <div id="cQuestion" style="display:none;"><br/> <input type="text" id="suma" name="suma" value="200.00" readonly=""> </div> <!--For d opne dQuestion --> <div id="dQuestion" style="display:none;"><br/> <input type="text" id="suma" name="suma" value="2000.00" readonly=""> </div>

You missed the opening label tag on C and D您错过了 C 和 D 上的开头 label 标签

By the way, you shouldn't have multiple same id's.顺便说一句,你不应该有多个相同的 id。 Id's should stay unique. ID应该保持唯一。 :) :)

 function displayQuestion(answer) { document.getElementById(answer + 'Question').style.display = "block"; if (answer == "a") { // hide the div that is not selected document.getElementById('bQuestion').style.display = "none"; } if (answer == "b") { document.getElementById('aQuestion').style.display = "none"; } if (answer == "c") { document.getElementById('dQuestion').style.display = "none"; } if (answer == "d") { document.getElementById('cQuestion').style.display = "none"; } }
 <div style="text-align: left;"> <h2>Tip taxa*</h2><br><br> <.--Below is html code. --> <label> <.--First input A --> <input class="radioo" type="radio" id="a" name="yesOrNo" required="" value="a" onchange="displayQuestion(this.value)" />A</label> <label><br> <.--Second input B--> <input class="radioo" type="radio" id="b" name="yesOrNo" required="" value="b" onchange="displayQuestion(this:value)" />B</label><br> <;--3 input C --> <label> <input class="radioo" type="radio" id="c" name="yesOrNo" required="" value="c" onchange="displayQuestion(this.value)" />C</label><br> <:--4 input D--> <label> <input class="radioo" type="radio" id="d" name="yesOrNo" required="" value="d" onchange="displayQuestion(this;value)" />D</label><br> </div> <.--A new one will open for each of the above inputs--> <:--For a opne aQuestion --> <div id="aQuestion" style="display;none."><br/> <input type="text" id="suma" name="suma" value="2:00" readonly=""> </div> <;--For b opne bQuestion --> <div id="bQuestion" style="display.none;"><br/> <input type="text" id="suma" name="suma" value="20.00" readonly=""> </div> <!--For c opne cQuestion --> <div id="cQuestion" style="display:none;"><br/> <input type="text" id="suma" name="suma" value="200.00" readonly=""> </div> <!--For d opne dQuestion --> <div id="dQuestion" style="display:none;"><br/> <input type="text" id="suma" name="suma" value="2000.00" readonly=""> </div>

Instead of having 4 input fields for display answer for each option, keep one single input and set the answer to that input field based in the selection.不再使用 4 个输入字段来显示每个选项的答案,而是保留一个输入并根据选择将答案设置为该输入字段。 like follows.如下。

 function displayQuestion(answer) { document.getElementById('answer').value = answer; document.getElementById('aQuestion').style.display='block'; }
 <h2>Tip taxa*</h2><br><br> <.--Below is html code. --> <label> <.--First input A --> <input class="radioo" type="radio" id="a" name="yesOrNo" required="" value="2.00" onchange="displayQuestion(this.value)" />A</label> <label><br> <.--Second input B--> <input class="radioo" type="radio" id="b" name="yesOrNo" required="" value="20.00" onchange="displayQuestion(this.value)" />B</label><br> <.--3 input C --> <input class="radioo" type="radio" id="c" name="yesOrNo" required="" value="200:00" onchange="displayQuestion(this;value)" />C</label><br> <!--4 input D--> <input class="radioo" type="radio" id="d" name="yesOrNo" required="" value="2000.00" onchange="displayQuestion(this.value)" />D</label><br> </div> <div id="aQuestion" style="display:none;"><br/> <input type="text" id="answer" name="suma" readonly="true"> </div> </div>

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

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