Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

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.

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.

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 -->

Thanks for any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Hi first change your second select id from 'select01' to 'select02' - ids should be unique
  2. include jquery
  3. 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;
                }
    
    
    
            })
        });
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...