[英]How can I populate a dropdownlist based on the selected value of another dropdownlist? MVC
[英]How can I populate the same value on the below dropdownlist
我的 controller 中有 2 個 viewbag
ViewBag.MyCategory = new SelectList(db.RecyclableType, "id", "type");
ViewBag.iddd = new SelectList(db.RecyclableType, "id", "type");
和一個視圖
@Html.DropDownList("iddd", (IEnumerable<SelectListItem>)ViewBag.iddd, "SELECT ITEM TYPE", new { @class = "form-control" })
@Html.DropDownList("MyCategory", (IEnumerable<SelectListItem>)ViewBag.MyCategory, "SELECT ITEM TYPE", new { @class = "form-control"})
我試圖創建一個 function 如下所示,但沒有任何效果
document.getElementById("iddd") = document.getElementById("MyCategory");
您可以使用 jQuery
iddd
下拉列表時獲取其value
MyCategory
下拉列表並將屬性設置為selected
$("#iddd").change(function () {
var value = $("#iddd").val();
$("#MyCategory").val(value).attr("selected", "selected");
});
注意:在head部分添加jQuery腳本文件
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.