2016-12-02 30 views
0

我有我的Dropdownbox名單.cshtml如下:asp.net mvc的顯示/隱藏DIV時Dropdownbox選項更改

<div class="form-group"> 
    @Html.Label("Role", new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.DropDownList("Name", null, new { @class = "form-control" }) 
    </div> 
</div> 

這是我第一次和第二div

<div class="StudentSection" style="display:none;"> 
Some contents 
</div> 

<div class="LecturerSection" style="display:none;"> 
Some contents 
</div> 

.js

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#StudentSection").hide(); 
    $("#LecturerSection").hide(); 

    $("#Name").change(function() { 
     if ($("#Name").val() == "Student") { 
      $("#StudentSection").show(); 
      $("#LecturerSection").hide(); 
     } 
     else { 
      $("#LecturerSection").show(); 
      $("#StudentSection").hide(); 
     } 
    }); 
}); 
</script> 

這些代碼不爲我的作品提供了我的代碼。

回答

0

變化都#StudentSection#LecturerSection.StudentSection.LecturerSection,因爲他們似乎是類 ID

看到這個工作的演示

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-group"> 
 
    <div class="col-md-10"> 
 
     <select id="Name"> 
 
    <option>Student</option> 
 
    <option>Lecturer</option> 
 
</select> 
 
    </div> 
 
</div> 
 
<div class="StudentSection" style="display:none;"> 
 
StudentSection Some contents 
 
</div> 
 

 
<div class="LecturerSection" style="display:none;"> 
 
LecturerSection Some contents 
 
</div> 
 

 
<script type="text/javascript"> 
 
$(document).ready(function() { 
 
    $(".StudentSection").hide(); 
 
    $(".LecturerSection").hide(); 
 

 
    $("#Name").change(function() { 
 
     if ($("#Name").val() == "Student") { 
 
      $(".StudentSection").show(); 
 
      $(".LecturerSection").hide(); 
 
     } 
 
     else { 
 
      $(".LecturerSection").show(); 
 
      $(".StudentSection").hide(); 
 
     } 
 
    }); 
 
}); 
 
</script>

+0

Thanksss了很多! :)這幫助我了! – H2O

+0

apvote答案,如果它幫助:) – jafarbtech