简体   繁体   中英

Jquery dynamic hide and show for drop down menu

I am having trouble making my drop down menu dynamically show blocks of html code which are labelled style="display:none" .

I have following code.

<script type="text/javascript">
  $(\'select[name="questiontype"]\').change(function(){

  if ($(this).val() == "multiple")
      alert("call the do something function on option multiple"); 
  else
      alert("call the do something function on option programming");
  });​
</script>

<form action="addQuestion.php" method="post">                       
  <select name="questiontype">                  
    <option name="questiontype" value="multiple" click="return showMultiple();">Multiple Choice< /option>    
    <option selected name="questiontype" value="programming" click="return showProgramming();">Programming< /option>                                
</select><br>

<input type="hidden" name="course" value="'.$course.'" />

<div id=\'multiple\' style="display:none">
   Multiple
</div>
<div id=\'programming\' style="display:none">
   Programming
</div>
</form>         

i tried these functions to .show() the div's by id from the dropdown menu but no luck and I'm not sure what I'm doing wrong. I also removed some code in the div id blocks to make it easier to read.

<script>
        function showMultiple(){
            $('#multiple').show();
            $('#programming').hide();
            return false;
        }
        function showProgramming(){
            $('#multiple').hide();
            $('#programming').show();
            return false;
        }
</script>

demo

HTML

<select id="selectMe">
    <option value="multiple">multiple</option>
    <option value="programming">Programming</option>
</select>
<br><br><br>

 <div id="multiple" class="group" >
   Multiple
</div>

 <div id="programming" class="group" >
   Programming
</div>

JS

$(document).ready(function () {
    $('.group').hide();
    $('#multiple').show();
    $('#selectMe').change(function () {
        $('.group').hide();
        $('#'+$(this).val()).show();
    })
});

In your select option you are using click instead of onclick event

<select name="questiontype">                  
    <option name="questiontype" value="multiple" onclick="return showMultiple();">Multiple Choice</option>    
    <option selected name="questiontype" value="programming" onclick="return showProgramming();">Programming< /option>                                
</select>

And no need to use escape for single quotes,

<div id='multiple' style="display:none">
   Multiple
</div>
<div id='programming' style="display:none">
   Programming
</div>

and in script tag

$('select[name="questiontype"]').change(function(){

Try this:

<form action="addQuestion.php" method="post">                       
            <select name="questiontype" id="questiontype">                  
                <option value="multiple">Multiple Choice</option>    
                <option selected value="programming">Programming</option>                                
            </select>

            <br>



            <div id='multiple' class="group" style="display:none">
                Multiple
            </div>
            <div id='programming' style="display:none"  class="group">
                Programming
            </div>
        </form>  

Put this code in head section:

<script src="jquery-1.9.1.min.js" type="text/javascript"></script>

        <script type="text/javascript">


            $(document).ready(function () {
                $('.group').hide();
                $('#programming').show();


                $('#questiontype').change(function () {
                    $('.group').hide(1000);
                    $('#'+$(this).val()).show(1000);
                })
            });

        </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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