我有一个下拉列表和一个actionlink。 当下拉列表更改时,将在其中自动单击此操作链接。怎么做?。下面是我的代码,谢谢。
@Html.DropDownListFor(model => model.PaymentCode, (List<SelectListItem>)ViewBag.JenisPembayarans, new { @class = "form-control" })
@Html.ActionLink(
"Detail",
"GetInsuranceCompany","ParamBlacklistPembayaran",
new { id = Model.PaymentCode }, new { @class = "ddlSubmit"})
控制器
public ActionResult GetInsuranceCompany( ParamBlacklistPembayaranViewModel model,string id)
{
LoadThirdPartyDDL(string.Empty, string.Empty, id);
return View("Create", model);
}
答案 0 :(得分:2)
@Html.DropDownListFor(model => model.PaymentCode, (List<SelectListItem>)ViewBag.JenisPembayarans, new { @class = "form-control",@id="ddl" })
@Html.ActionLink("Detail",
"GetInsuranceCompany","ParamBlacklistPembayaran",
new { id = "PaymentCodeVal" }, new { @id="anchorclick",@class = "ddlSubmit"})
您应该像这样在下拉更改中调用click事件:
<script>
document.getElementById('ddl').onchange = function () {
var path = document.getElementById('anchorclick').href;
path = path.replace("PaymentCodeVal", document.getElementById('ddl').value);
document.getElementById("anchorclick").href=path;
document.getElementById('anchorclick').click();
};
</script>
@注意::您要获取更新的PaymentCode。您必须注入网址以在发生更改事件时传递PaymentCode。
答案 1 :(得分:1)
在新的{}部分分配onchange事件,您可以在其中使用其ID引发特定操作链接的事件。
@Html.DropDownListFor(model => model.PaymentCode, (List<SelectListItem>)ViewBag.JenisPembayarans, new { @class = "form-control", @id = "MyId", onchange = "MyFunction()" })
<script type="text/javascript">
function MyFunction() {
//alert('Changed');
document.getElementsByClassName("ddlSubmit").click();
$('#YourLabelId').val('ReplaceWithThisValue');
}
</script>
参考文献:
Handling onchange event in HTML.DropDownList Razor MVC