简体   繁体   English

从下拉菜单创建复选框

[英]Creating checkboxes from drop-down menu

I've tried to look up a working solution for my problem, but after days of research a couldn't fine one, and that's why i'm asking for your help. 我试图为我的问题寻找一种可行的解决方案,但是经过数天的研究,仍然无法解决问题,这就是为什么我寻求您的帮助。

Basically, what i would like to do is to have a drop down menu, with several year group options in it. 基本上,我想做的是有一个下拉菜单,其中包含若干年的组选项。 So for example the drop down menu would have year 7, year 8 year 9 etc inside of it. 因此,例如,下拉菜单中将包含第7年,第8年,第9年等。

Once they select year 7, the system would create only one checkbox. 一旦他们选择了7年级,系统将只创建一个复选框。 For year 8 and 9 there should be two checkboxes created, because they can choose either one or two subjects. 对于8年级和9年级,应该创建两个复选框,因为它们可以选择一个或两个主题。

If the drop down menu is not changed, there should be no checkboxes. 如果下拉菜单未更改,则不应有任何复选框。

I hope these all make sense and you guys will be able to give me an answer. 我希望所有这些都有道理,你们将能够给我答案。 Thanks in advance. 提前致谢。

There is a lot of Google answers, dozen jQuery plugins and SO answers already asked and resolved about it. 已经有很多Google答案,许多jQuery插件SO答案已被问及解决。 Why it's not acceptable for you or what exclusively you need? 为什么不为您接受或您仅需要什么?

Something like this then: 然后是这样的:

 var subjectMap = { 'Year 7' : ['Maths'], 'Year 8' : ['Maths', 'English'], 'Year 9' : ['Maths', 'English', 'French'], 'Year 10' : ['French', 'History', 'Geography'] } $( document ).ready(function() { for (var year in subjectMap) { $('#yearGroup').append($('<option>', {value:year, text:year})); } $('#yearGroup').change(function () { var selectedYear = $("#yearGroup option:selected").val(); $("input:radio[name='subjects']").hide().attr('checked', false); $("input:radio[name='subjects']").each(function(){ $("#label"+ $(this).val()).hide(); }); for (var availableSubject in subjectMap[selectedYear]) { $("#" + subjectMap[selectedYear][availableSubject]).show(); $("#" + subjectMap[selectedYear][availableSubject]).each(function(){ $("#label"+ $(this).val()).show(); }); } }).change(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id='yearGroup'></select><br> <label for="Maths" id="labelMaths">Maths</label><input type="radio" name="subjects" value="Maths" id="Maths"> <label for="English" id="labelEnglish">English</label><input type="radio" name="subjects" value="English" id="English"> <label for="French" id="labelFrench">French</label><input type="radio" name="subjects" value="French" id="French"> <label for="History" id="labelHistory">History</label><input type="radio" name="subjects" value="History" id="History"> <label for="Geography" id="labelGeography">Geography</label><input type="radio" name="subjects" value="Geography" id="Geography"> 

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

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