繁体   English   中英

组合框值更改时如何触发按钮

[英]How to trigger a button when combo box value changed

在我的 ASP.NET MVC 应用程序中,有一个包含主要请求类型的组合框。

当用户更改组合框值时,显示和隐藏与其相关的部分视图。

我想知道每个局部视图都包含按钮,当它单击时,包含在局部视图中加载。 那么在更改组合框值时有什么方法可以做到这一点,默认情况下我想加载包含加载。 我可以像局部视图一样加载它,但是对于这个程序,有必要单击按钮,因为用户单击的每个局部视图都带有来自控制器的参考编号。

这是组合框的编码方式。

<div class="form-group row">
    @Html.LabelFor(model => model.ReqType, htmlAttributes: new { @class = "control-label col-md-3" })
    <div class="col-sm-8">
        @Html.DropDownListFor(model => model.ReqType, ReqTypes, "Select Request Type", new { @class = "js-dropdown", id = "ReqType" })
        @Html.ValidationMessageFor(model => model.ReqType, "", new { @class = "text-danger" })
    </div>
</div>

这就是我根据组合框选定值隐藏和显示部分视图的方式

$('#ReqType').change(function () {

      if ($(this).val() == '1') {
        $('#pnlPurchaseEmp').show();
        $('#pnlPurchaseItem').show();
        $('#pnlGeneralItms').hide();
        $('#pnlSuspenseDetails').hide();
        $('#SusMain').empty();
        $('#PayVMain').empty();
        $('#ReqSettle').empty();
        $('#PayVExpen').empty();
        $('#PayBill').empty();
        $('#PayBillS').empty();
      }
} 

所以这是包含加载部分的部分视图,例如,如果combobox value ==1 ,则$('#pnlPurchaseEmp').show(); 这将显示并且这个按钮在那里,所以当$('#pnlPurchaseEmp').show(); 这开始显示我也想触发这个addAnotherEmp按钮

<fieldset id="pnlPurchaseEmp" style="display:@(Model.PurchasingEmpList == null || Model.PurchasingEmpList.Count == 0 ? " none" : "" )">
  <ul id="RequEmp" style="list-style-type: none"> @if (Model != null && Model.PurchasingEmpList != null) { foreach (Asp_PASMVC.Models.PurchasingEmpl Emp in Model.PurchasingEmpList) { Html.RenderPartial("_PurchaseEmployees", Emp); } } </ul>
  <button type="button" id="addAnotherEmp" class="btn btn-info" href="#">Add New Record</button>
  <script type="text/javascript">
    $(function() {
      $("#addAnotherEmp").click(function() {
        $.get('/AppRequests/PurchaseEmpList', function(template) {
          $("#RequEmp").append(template);
        });
      });
    });
  </script>
  <br />
</fieldset>

试试这个

$('#ReqType').change(function () {
  if ($(this).val() == '1') {
    $('#pnlPurchaseEmp').show(0, 
        function() {// show with callback function
          $('#addAnotherEmp').trigger('click');
        });         
     }
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM