使用javascript从选择下拉列表中选择默认值时隐藏div

时间:2017-05-19 01:04:00

标签: javascript jquery html

我选择了一个默认值为“选择位置”的下拉列表。选择任何位置后,将显示学校名称的隐藏div。现在,如果从第一个下拉选择中再次选择“选择位置”,我将如何自动隐藏它?

页面来源

     <div class="col-lg-12">
        <div class="panel panel-default">
          <div class="panel-body">
           <div class="form-group">
            <div class="col-lg-2">
               <h3>Primary</h3>
            </div>
               <div class="col-lg-3">
               <div class="form-group">
<label class="control-label">School Location:</label>
<select name="location_primary" class="form-control locationDropdown" id="selection"><option>Choose location</option>                                                                                           <option  value="4" rel="4">Abra</option>                                                                                              <option  value="5" rel="5">Agusan del Norte</option>                                                                                             <option  value="6" rel="6">Agusan del Sur</option>                                                                              <option  value="7" rel="7">Aklan</option>                                                                                               <option  value="8" rel="8">Albay</option>                                                                                              <option  value="9" rel="9">Antique</option>                                                                                               <option  value="10" rel="10">Apayao</option>                                                                                               <option  value="11" rel="11">Aurora</option>                                                                                               <option  value="12" rel="12">Basilan</option>                                                                                                <option  value="13" rel="13">Bataan</option>                                                                                              <option  value="14" rel="14">Batanes</option>                                                                                                <option  value="15" rel="15">Batangas</option>                                                                                                <option  value="16" rel="16">Benguet</option>                                                                                                <option  value="17" rel="17">Biliran</option>                                                                                                <option  value="18" rel="18">Bohol</option>                                                                                                <option  value="19" rel="19">Bukidnon</option>                                                                                                <option  value="20" rel="20">Bulacan</option>                                                                                              <option  value="21" rel="21">Cagayan</option>                                                                                        </select>
</div>

<div class="form-group schools" id="showDiv">
 <label class="control-label">Name Of School </label>
   <button class=" add_field_button_primary btn-primary">+</button>
      <select name="school_primary" class="form-control ">
        <option  class="0">Choose school</option>                                                                                              <option  value="8" class="63">Abada College</option>                                                                                                <option  value="9" class="57">ABE International Coll of Business and Economics-Bacolod</option>                                                                                               <option  value="10" class="48">Abuyog Community College</option>                                                                                               <option  value="11" class="82">Abubakar Computer Learning Center Foundation</option>                                                                                               <option  value="12" class="20">Academia de San Lorenzo Dema Ala</option>                                                                                              <option  value="13" class="4">Abraham Dumlao Colleges of the Philippines</option>                                                                                               <option  value="14" class="5">ACLC College</option>                                                                                                <option  value="15" class="6">Agusan Del Sur State College of Agriculture and Technology</option>                                                                                                <option  value="16" class="6">Agusan Del Sur College</option>                                                                                               <option  value="17" class="7">Aklan Polytechnic Institute</option>                                                                                                <option  value="18" class="7">Aklan Catholic College</option>                                                                                              <option  value="19" class="8">Albay Institute of Technology</option>                                                                                               <option  value="20" class="8">Bicol University-College of Agriculture and Forestry</option>                                                                                                                                                                           </select>
</div>

**** **** HTML

    <div class="col-lg-12">
       <div class="panel panel-default">
          <div class="panel-body">
             <div class="form-group">
              <div class="col-lg-2">
               <h3>Primary</h3>
             </div>

              <div class="col-lg-3">
             <div class="form-group">
             <label class="control-label">School Location:</label>
             <select name="location_primary" class="form-control" id="selection">
             <option>Choose location</option>
              @foreach($locations as $location)
             <option  value="{!! $location->location_id!!}" rel="{!! $location->location_id!!}">{!! $location->location_name!!}</option>
              @endforeach
            </select>
             </div>

             <div class="form-group" id="showDiv">
             <label class="control-label">Name Of School </label>
             <button class=" add_field_button_primary btn-primary">+</button>
             <select name="school_primary" class="form-control " style="display: none;">
             <option  class="0">Choose school</option>
                @foreach($schol as $school)
             <option  value="{!! $school->school_id !!}" class="{!! $school->location_id!!}">{!! $school->school_name!!}</option>
                @endforeach
             </select>
             </div>



          <div class="input_fields_view_primary">
          </div>
         </div> 
       <div class="col-lg-4">
         <label class="control-label">SY Graduated:</label> 
            {!! Form::hidden('sy', '', array('id' => 'sy_hidden')) !!}
             <select class="form-control" name="scholar_primary_school_year" id="pri_sy" onchange="educPre(this)">

               <?php 
               $null = 'Null';
               $choose = 'Choose A Year';
               $Present = 'Present';
               echo '<option value='.$null.'>' .$choose.'</option>';
               echo '<option value='.$Present.'>' .$Present.'</option>';
                  for($i=date('Y');  $i > date('Y')-27; $i--){
                                            $x = $i +1;
                                            $y = $i;
            echo '<option value='.$y.'-'.$x.'>'.$y.'-'.$x.'</option>';
                }?>
             </select>  
        <div class="form-group" id="hidden_div_Pre" style="display: none;" >
           <div class="form-group"><br>
            <label class="control-label">Select Grade Level:</label>
            {!! Form::select('scholar_primary_grade', [
                                            ''=>'Choose a Year Level',
                                            '1' => 'Grade-1',
                                            '2' => 'Grade-2',
                                            '3' => 'Grade-3',
                                            '4' => 'Grade-4',
                                            '5' => 'Grade-5',
                                            '6' => 'Grade-6'
                                            ],NULL,['class'=>'form-control']) !!}
                                        </div>
       <div class="form-group">
       <label class="control-label">Grade Point Average (GPA):</label>
          {!! Form::text('scholar_primary_gpa',NULL,['class'=>'form-control','placeholder'=>'Enter Your Last Grade Point Average'])!!}
        </div> 
         <div class="form-group">
        <label class="control-label">Grade Picture:</label>
       {!! Form::file('scholar_primary_grade_proof',['class'=>'form‐control','id'=>'scholar_primary_grade_proof'  ]) !!}
                  </div> 
        <div class="form-group">
        <img id="certificate" src="#" name="scholar_primary_grade_proof"  class="img-responsive well" alt="Image" />
                                        </div>
                                    </div>
                                </div> 
     <div class="col-lg-3">
      <div class="form-group">
     <label class="control-label">Award and Recognition:</label>
      {!! Form::text('scholar_primary_awards',NULL,['class'=>'form-control','placeholder'=>'Enter Your Award and Recognition','data-role'=>'tagsinput'])!!}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

****** ******脚本

 $(document).ready(function(){
    var $cat = $('select[name=location_primary]'),
    $school = $('select[name=school_primary]');
var thisSelect = document.getElementById('selection');
    var selection = thisSelect.options[thisSelect.selectedIndex].innerHTML;
        if(selection == 'Choose location'){
                $('#showDiv').hide();
           }

         $cat.change(function(){
           if(selection == 'Choose location') {
                $('#showDiv').show();
           } 


           var $this = $(this).find(':selected'),
           rel = $this.attr('rel'),
           $set = $school.find('option.' + rel);

            if ($set.size() < 0) {
              $school.hide();
              return;
            }
      $school.show().find('option').hide();
      $set = $school.find('option.0, option.' + rel);
      $set.show().first().prop('selected', true);
  });
});

2 个答案:

答案 0 :(得分:1)

这是你在找什么?很难解密你的代码(与PHP混合),但我认为如果你改变一些名字,这应该有效。

&#13;
&#13;
$(document).on("change", ".locationDropdown", function(e) {
  //Value of the select
  var selectValue = $(this).val();
  
  //If Choose Location is chosen, hide div
  if (selectValue == 'Choose location') {
    $(".location").hide();
  } else {
    $(".location").html(selectValue);
    $(".location").show();
  }

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class='locationDropdown'>
  <option value="Choose location" selected>Choose location</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<div class='location'></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

$cat.change内使用this获取当前选定的值,并根据当前值隐藏和显示div。

&#13;
&#13;
$(document).ready(function(){
        var $cat = $('select[name=location_primary]'),
            $school = $('select[name=school_primary]');
        var thisSelect = document.getElementById('selection');
        var selection = thisSelect.options[thisSelect.selectedIndex].innerHTML;
        if(selection == 'Choose location'){
            $('#showDiv').hide();
        }

        $cat.change(function(){
            if($(this).find(":selected").text() == 'Choose location') {
                $('#showDiv').hide();
            }else {
                $('#showDiv').show();
            }


            var $this = $(this).find(':selected'),
                rel = $this.attr('rel'),
                $set = $school.find('option.' + rel);

            if ($set.size() < 0) {
                $school.hide();
                return;
            }
            $school.show().find('option').hide();
            $set = $school.find('option.0, option.' + rel);
            $set.show().first().prop('selected', true);
        });
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="location_primary" class="form-control" id="selection">
    <option>Choose location</option>
    <option value="la">LA</option>
    <option value="some">some</option>
</select>

<div class="form-group" id="showDiv">
    <label class="control-label">Name Of School </label>
    <button class=" add_field_button_primary btn-primary">+</button>
    <select name="school_primary" class="form-control " style="display: none;">
        <option  class="0">Choose school</option>
        <option value="schoool">School</option>
        <option value="anotherschoool">Another School</option>
    </select>
</div>
&#13;
&#13;
&#13;