简体   繁体   English

隐藏/显示 Div 代码不适用于下拉框选项

[英]Hide/Show Div code not working for dropdown box choices

I know that this question is asked many times and I actually got the answer from another post, however now I have written the code to suit my needs it is not showing the div at all when clicking on the specific dropdown choice.我知道这个问题被问了很多次,实际上我从另一篇文章中得到了答案,但是现在我已经编写了适合我需要的代码,当点击特定的下拉选项时,它根本没有显示 div。 I really liked this method as the code made more sense to me but am not sure what I am doing wrong.我真的很喜欢这种方法,因为代码对我来说更有意义,但不确定我做错了什么。 I am setting up a form and want to have a few questions where the user will choose an option and then a specific question will then show below asking one or more specific questions.我正在设置一个表单,并希望有几个问题,用户将在其中选择一个选项,然后一个特定的问题将显示在下面,询问一个或多个具体问题。 I did this with Yes/No radio buttons elsewhere which is working, but I need to do this with three options that will lead to three separate inquiries to get more information, and another choice where they have the option to just leave it as is and not answer.我在其他地方使用“是/否”单选按钮执行此操作,但我需要使用三个选项来执行此操作,这将导致三个单独的查询以获取更多信息,以及另一个选择,他们可以选择保持原样和不回答。

The code for the javascript is as follows: javascript的代码如下:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>">
    <script>

function landaccessfunction(){
                        getValue = document.getElementById("access").value;
                        if(getValue == "road"){
                            document.getElementById("roaddetails").style.display = "block";
                            document.getElementById("easementdetails").style.display = "none";
                            document.getElementById("noaccessdetails").style.display = "none";
                        }
                        if(getValue == "easement"){
                            document.getElementById("roaddetails").style.display = "none";
                            document.getElementById("easementdetails").style.display = "block";
                            document.getElementById("noaccessdetails").style.display = "none";
                        }
                        if(getValue == "noaccess"){
                            document.getElementById("roaddetails").style.display = "none";
                            document.getElementById("easementdetails").style.display = "none";
                            document.getElementById("noaccessdetails").style.display = "block";
                        }
                        if(getValue == "selectaccess"){
                            document.getElementById("roaddetails").style.display = "block";
                            document.getElementById("easementdetails").style.display = "block";
                            document.getElementById("noaccessdetails").style.display = "block";
                        }
                    }
                </script>

Then this is the part of the code which is placed within the body tags and the form itself:然后这是放置在正文标签和表单本身中的代码部分:

How is the land accessed?
<select style="height:24px;font-size:12pt; name="access" id="access" onchange="landaccessfunction" class="form-control" form="landform">
                <option value="selectaccess">Select One</option>
                <option value="road">Direct road access</option>
                <option value="easement">A legally deeded easement</option>
                <option value="noaccess">No legal access</option>
                          </select> 


<div class=text" id="roaddetails" style="display:none;">                
Who owns the road?<br><input style="height:20px;font-size:12pt; name="roadowner" type="text" id="roadowner" form="landform" size="50">
</div>

<div class=text" id="easementdetails" style="display:none;">                
Describe easement<br><input style="height:20px;font-size:12pt; name="easedescr" type="text" id="easedescr" form="landform" size="50">
</div>

<div class=text" id="noaccessdetails" style="display:none;">                
How do you access the land at this time?<br><input style="height:20px;font-size:12pt; name="howaccess" type="text" id="howaccess" form="landform" size="50">
</div>

My apologies if it is something stupid that I am not seeing.如果这是我没有看到的愚蠢的东西,我深表歉意。 Also note that the questions that show up may include text boxes or other form field types but for now I have just put simple short entry fields.另请注意,显示的问题可能包括文本框或其他表单字段类型,但现在我只放置简单的简短输入字段。

Thank you so much.太感谢了。

Your code has some issues.您的代码有一些问题。 Below is the running code:下面是运行代码:

 function landaccessfunction(){ getValue = document.getElementById("access").value; if(getValue == "road"){ document.getElementById("roaddetails").style.display = "block"; document.getElementById("easementdetails").style.display = "none"; document.getElementById("noaccessdetails").style.display = "none"; } if(getValue == "easement"){ document.getElementById("roaddetails").style.display = "none"; document.getElementById("easementdetails").style.display = "block"; document.getElementById("noaccessdetails").style.display = "none"; } if(getValue == "noaccess"){ document.getElementById("roaddetails").style.display = "none"; document.getElementById("easementdetails").style.display = "none"; document.getElementById("noaccessdetails").style.display = "block"; } if(getValue == "selectaccess"){ document.getElementById("roaddetails").style.display = "block"; document.getElementById("easementdetails").style.display = "block"; document.getElementById("noaccessdetails").style.display = "block"; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> How is the land accessed? <select style="height:24px;font-size:12pt;" name="access" id="access" onChange="landaccessfunction()" class="form-control" form="landform"> <option value="selectaccess">Select One</option> <option value="road">Direct road access</option> <option value="easement">A legally deeded easement</option> <option value="noaccess">No legal access</option> </select> <div class="text" id="roaddetails" style="display:none;"> Who owns the road?<br><input style="height:20px;font-size:12pt;" name="roadowner" type="text" id="roadowner" form="landform" size="50"> </div> <div class="text" id="easementdetails" style="display:none;"> Describe easement<br><input style="height:20px;font-size:12pt;" name="easedescr" type="text" id="easedescr" form="landform" size="50"> </div> <div class="text" id="noaccessdetails" style="display:none;"> How do you access the land at this time?<br><input style="height:20px;font-size:12pt;" name="howaccess" type="text" id="howaccess" form="landform" size="50"> </div>

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

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