2012-11-06 98 views
1

當我在下拉列表中選擇調整時,我可以獲取值,但是當呈現正確的表單時,表單中的下拉UI沒有正確的值。它幾乎可行,但我覺得我沒有正確地做jquery。在視圖中,我有兩種不同的表單,我使用Jquery根據下拉選擇選擇要顯示的表單。根據MVC和Jquery中的下拉選擇隱藏或顯示特定形式

繼承人JQUERY

$(document).ready(function() { 
     $("#adjustments").hide(); 
     $(".TypeClass").change(function() { 
      var selected = $(".TypeClass option:selected").text(); 
      alert(selected); 
      if (selected == "Received") { 
       $("#received").show(); 
       $("#adjustments").hide(); 
      } 
      if (selected == "Adjustment") { 
       $("#adjustments").show(); 
       $("#received").hide(); 
      } 
     }); 


     $(".TypeClass2").change(function() { 
      var selected = $(".TypeClass2 option:selected").text(); 
      alert(selected); 
      if (selected == "Received") { 
       $("#received").show(); 
       $("#adjustments").hide(); 
           } 
      if (selected == "Adjustment") { 
       $("#adjustments").show(); 
       $("#received").hide(); 
           } 

     }); 
    }); 

繼承人的VIEW

<div id="received"> 
    <fieldset> 
    <legend>New Inventory Transaction</legend> 
    <table> 
     <tr><td>@Html.LabelFor(model => model.ItemName)</td><td>@Html.DisplayFor(model => model.ItemName)</td></tr> 
     <tr><td>@Html.LabelFor(model => model.ItemNumber)</td><td>@Html.DisplayFor(model => model.ItemNumber)</td></tr> 
     <tr><td>@Html.LabelFor(model => model.XactTypeId)</td><td>@Html.DropDownListFor(model => model.XactTypeId, (SelectList)ViewData["InvType"], "Choose Type", new { @class = "TypeClass" })</td></tr> 
     <tr><td>@Html.LabelFor(model => model.XactTotalCost)</td><td>@Html.EditorFor(model => model.XactTotalCost)</td></tr> 
     <tr><td></td><td><p> <input type="submit" value="Create" /></p></td></tr>  
     </table> 
     </fieldset> 
     </div>  
     <div id="adjustments"> 
     <fieldset> 
     <legend>New Inventory Transaction</legend> 
     <table> 
     <tr><td>@Html.LabelFor(model => model.ItemName)</td><td>@Html.DisplayFor(model => model.ItemName)</td></tr> 
     <tr><td>@Html.LabelFor(model => model.ItemNumber)</td><td>@Html.DisplayFor(model => model.ItemNumber)</td></tr> 
     <tr><td>@Html.LabelFor(model => model.WharehouseName)</td><td>@Html.EditorFor(model => model.WharehouseName)</td></tr>  
     <tr><td></td><td><p> <input type="submit" value="Create" /></p></td></tr> 

     </table> 

     </fieldset> 
     </div> 
+0

你正面臨什麼問題? –

+0

在UI部分進行測試時,下拉列表與我設置的ALERTS不匹配。是否有一些jquery,將允許我設置它像這樣在C#dropdownlist1.SelectedValue =「myValue」 –

回答

1

設置一個下拉列表值只使用
$( 「DDL」)VAL( 「你VAL」)。
並獲得選定的值usev
var selectedval = $(「ddl」)。val();

0

下拉

@Html.DropDownListFor(m => m.AttendenceBonus.BonusIn, new SelectList(Model.BonusInList, "Value", "Text", Model.AttendenceBonus.BonusIn), new { @class = "selectBoxRegular", onchange = "return GetBonusType(this);"}) 

腳本

<script> 
    function GetBonusType(ctrl) { 

     var x = $("#AttendenceBonus_BonusIn").val(); 

     // hide any control by id 

      if (x == "Fixed") { 
       $('#persent').text(""); 
       $('#Applylevel').show(); 
       $('#AttendenceBonus_BonusApplyOn').show(); 
      } 

     // Show any control by id 
      else { 
       $('#persent').text("%"); 
       $('#Applylevel').hide(); 
       $('#AttendenceBonus_BonusApplyOn').hide(); 
      } 
    } 

</script> 

You can also see the GIF

相關問題