首页 > 解决方案 > 如何在下拉列表更改时自动单击 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);
}

标签: javascriptasp.net-mvc

解决方案


 @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。您必须注入 url 才能在 change 事件中传递 PaymentCode。


推荐阅读