使用jQuery向表单添加另一个下拉列表 [英] Adding another dropdown list to a form with jQuery
问题描述
在第一个下拉菜单中选择一个选项后,我正尝试向表单添加另一个下拉列表.
I'm trying to add another dropdown list to a form once an option is selected in the first dropdown.
因此,下面是表格的一部分.当选择具有名称的下拉,在这种情况下有3类,EDMATH 502,EDTECH 401,数学101,我想不同图像ID出现在下一下拉(image_os_image_id").想法是每个班级都可以访问不同的图像.
So below is part of the form. When the dropdown with the name is selected, in this case there are 3 classes, EDMATH 502, EDTECH 401, Math 101, I want different image IDs to appear in the next dropdown ("image_os_image_id"). The idea being that each class has access to different images.
我知道这是jquery的工作,但是我花了一点时间来弄清楚该怎么做.我已经搜索了很多,但是由于搜索不佳或我似乎总是想为将其他选项添加到列表而不是添加其他列表而受到打击.
I know this is a job for jquery, but I am having a heck of a time figuring out how to do that. I have searched quite a bit, but am stymied a bit by either poor searching or the fact that I always seem to come up with hits for adding another option to a list, not adding another list.
<div class="control-group">
<label class="control-label" for="select01">Class</label>
<div class="controls">
<select id="select01" name="class_name">
<option value='EDMATH 502'>EDMATH 502</option>
<option value='EDTECH 401'>EDTECH 401</option>
<option value='Math 101'>Math 101</option>
</select>
</div>
<!-- class -->
</div>
<div class="control-group" </div>
<label class="control-label" for="select01">Image</label>
<div class="controls">
<select id="select01" name="image_os_image_id">
<option value='647abf63-fd95-42a7-a744-e1885f8d5c16'>photoshop</option>
<option value='0f53de4a-1bb6-43bd-a567-fe181b25cfbe'>matlab</option>
<option value='647abf63-fd95-42a7-a744-e1885f8d5c16'>photoshop</option>
<option value='0f53de4a-1bb6-43bd-a567-fe181b25cfbe'>matlab</option>
</select>
</div>
<!-- image -->
</div>
<!-- control group -->
感谢您的帮助!
推荐答案
- 您好,先将您的第二个选择ID从'select01'更改为'select02'-ID应该是唯一的
- 包括jquery
-
添加脚本
- Hi first change your second select id from 'select01' to 'select02' - ids should be unique
- include jquery
add the script
$(document).ready(function(){
$('#select01').bind('change', function(){
$('#select02 option').remove();
switch($(this).val()) {
case 'EDMATH 502':
$('#select02').append('<option value="647abf63-fd95-42a7-a744-e1885f8d5c16">photoshop</option>');
$('#select02').append('<option value="0f53de4a-1bb6-43bd-a567-fe181b25cfbe">matlab</option>');
$('#select02').append('<option value="647abf63-fd95-42a7-a744-e1885f8d5c16">photoshop</option>');
$('#select02').append('<option value="0f53de4a-1bb6-43bd-a567-fe181b25cfbe">matlab</option>');
break;
case 'EDTECH 401':
$('#select02').append('<option value="test1">test1</option>');
$('#select02').append('<option value="test2">test11</option>');
break;
case 'Math 101':
$('#select02').append('<option value="test2">test2</option>');
$('#select02').append('<option value="test22">test22</option>');
break;
}
})
});
这篇关于使用jQuery向表单添加另一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!