[英]How to get a @html.Checkbox to update a @html.dropdownList
我有以下復選框:
@Html.CheckBox("Norm", false, new { @class = "checkbox" })
選中后,我希望它自動更新我的下拉菜單
@Html.DropDownList("selectedCore", (SelectList)ViewBag.CoreSheets)
像這樣從我的控制器中填充下拉選擇列表,
ViewBag.CoreSheets = new SelectList(db.CoreSheets, "sheetID", "Abb");
因此,總而言之,我想單擊復選框並更新當前的下拉值。
干杯,
BNJ
試試這個代碼
@Html.CheckBox("Norm", false, new { @class = "checkbox",@onclick="clickNorm()" })
<script type="text/javascript">
function clickNorm(){
$.ajax({
url:'@Url.Action("LoadSelectedCoreList")'
dataType:'json',
success:function(res){
if(res && res.length>0){
$('#selectedCore).empty();
$.each(res,function(index,item){
$('#selectedCore').append(new Option(item.Abb,item.sheetID));
});
}
}
});
</script>
在Coltroller中
public ActionResult LoadSelectedCoreList()
{
var selectedCoreList=..
return Json(selectedCoreList,
JsonRequestBehavior.AllowGet);
}
我通過以下方法自己解決了這個問題,
復選框
@Html.CheckBox("Norm", false, new { @class = "checkbox" })
jQuery的
<script type="text/javascript">
$(document).ready(function() {
$("#Norm").on("click", function () {
if ($(this).is(":checked")) {
$("#kammcoreauto").val(1);
$("#kammfaceauto").val(1);
$("#spacecoreauto").val(1);
$("#Washercoreauto").val(1);
$("#IsoCoreauto").val(1);
$("#IsoFaceauto").val(1);
}
});
});
</script>
這只是指您可以像這樣添加的下拉菜單的ID
@Html.DropDownList("selectedCore", (SelectList)ViewBag.CoreSheets, new { id = "kammcoreauto" })
非常感謝所有嘗試提供幫助的人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.