admin管理员组文章数量:1302404
I'm trying to program a website using HTML and I have a question. I'm using a drop-down list with options A and B, but after one of this is chosen, I'd like to create another drop down list and text fields at the same page, with different questions. For example, If some one choose A, then it'll be asked questions C and D, and if someone chose B, it'll be asked questions E and F. What is the best way to program this ?
Sorry for any mistake in English, I'm not a native speaker. And Thanks in advance.
I'm trying to program a website using HTML and I have a question. I'm using a drop-down list with options A and B, but after one of this is chosen, I'd like to create another drop down list and text fields at the same page, with different questions. For example, If some one choose A, then it'll be asked questions C and D, and if someone chose B, it'll be asked questions E and F. What is the best way to program this ?
Sorry for any mistake in English, I'm not a native speaker. And Thanks in advance.
Share edited Dec 23, 2010 at 19:53 Marcelo asked Dec 23, 2010 at 19:50 MarceloMarcelo 1,5532 gold badges15 silver badges16 bronze badges 4- You will need javascript for that. The answer might be a bit plex though. – yoda Commented Dec 23, 2010 at 19:54
- 1 You can check stackoverflow./questions/3637972/… if there is dynamic data (fetched from database) involved. Check my answer to the same question stackoverflow./questions/3637972/… – Sandeepan Nath Commented Dec 23, 2010 at 19:55
- Hi, I'm a beginner and I'm using simple basic stuffs like <select name=""> <option value=""></option> – Marcelo Commented Dec 23, 2010 at 19:56
- These "stuffs" are html tags. As it is said before you'll need javascript. I remend using jquery library (jquery.). It will make things shorter and easier. – Javi Commented Dec 23, 2010 at 20:00
4 Answers
Reset to default 6It depends on whether the data is static (i.e. coded on the page and will not change) or dynamic (i.e. delivered from the web server).
If your drop-down lists are supposed to only filter their contents based on List A, then you will probably want to use some JavaScript to hide / remove the items that you don't want. Consider looking at jQuery for this as it's an extremely easy to use and understand framework and there are plenty of good examples both on Stack Overflow and throughout the Internet that you can refer to.
If your data needs to be delivered from a web server (i.e. you have a database that you're referring to for the list values) then you'll need to provide more information about your programming framework (e.g. ASP.Net, PHP, etc) and how you want the lists to work (Ajax, full-page postback, etc).
Let's assume you're keeping everything static on the page and hiding information based on the user's selection.
Your HTML might look something like this:
<body>
<div id="myQuestions">
<select id="QuestionOptions">
<option value="A">Question A</option>
<option value="B">Question B</option>
</select>
</div>
<div id="myAnswers">
<div id="A" style="display: none;">
<div id="QuestionC">
<p>Here is an example question C.</p>
</div>
<div id="QuestionD">
<select id="QuestionOptionsD">
<option value="G">Question G</option>
<option value="H">Question H</option>
</select>
</div>
</div>
<div id="B" style="display: none;">
<div id="QuestionE">
<p>Here is an example question E.</p>
</div>
<div id="QuestionF">
<select id="QuestionOptionsF">
<option value="I">Question I</option>
<option value="J">Question J</option>
</select>
</div>
</div>
</div>
</body>
Your JavaScript (I'm using jQuery, as mentioned above) could then look like this:
$(function () {
$('#QuestionOptions').change(function () {
$('#myAnswers > div').hide();
$('#myAnswers').find('#' + $(this).val()).show();
});
});
You could do it using javascript appendChild()
(Example Here) and append the div dynamically based on which option is chosen. Or you could have the other selects static and have them hidden with css, then display which one you want depeding on which option is chosen.
Have a hidden dropdown that corresponds to each option in the first dropdown. Use the onclick handler on the first dropdown to change the corresponding hidden dropdown's hidden attribute.
jQuery might make your life easier on this one.
I might not understand your question, but it sounds like you might be overplicating things. If you are simply trying to prompt two separate sets of questions you don't need javascript, you can just have two separate pages with the different questions. A -> links to a page w/ questions C/D while B-> links to E/F.
You might try posting some code and even a demo for interface questions... it's a very good way to help articulate your question.
本文标签: javascriptConditional simple drop down listStack Overflow
版权声明:本文标题:javascript - Conditional simple drop down list? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741692759a2392827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论