【问题标题】:Checkbox change event not working inside partial view mvc复选框更改事件在部分视图 mvc 中不起作用
【发布时间】:2013-11-28 12:13:21
【问题描述】:
I have one partial view page having 3 radio buttons namely All Employees,Designation & Confirmation Date.By default first is selected.on click of third radio button i need to enter date in textbox after that ajax call is made and data filtered by date is renderd in div tag.
///the following is code for that partial view

@model HRMS.Models.EligibilityCriteriaModel
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("../../Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
@{
    ViewBag.Title = "Index";    
}
@Html.HiddenFor(modelItem => modelItem.allConfirmationDateEmployeeListCount, new { id = "ConfirmationDateEmpListCount" })
@if (Model.allConfirmationDateEmployeeList != null)
{
<table cellpadding="0" cellspacing="0" border="0" width="100%" id="tbl_allEmployees"
    class="TablesBlueHeaders">
    <thead>
        <tr>
            <th width="10%" class="bluebgtable1">
                Employee Code
            </th>
            <th class="bluebgtable1" width="15%">
                Employee Name
            </th>
            <th class="bluebgtable1" width="15%">
                Delivery Team
            </th>
            <th class="bluebgtable1" width="15%">
                Designation
            </th>
            <th class="bluebgtable1" width="15%">
                Confirmation Date
            </th>
            <th class="bluebgtable1" width="15%">
                Select
            </th>
        </tr>
    </thead>
    <div class="abc" id="SampleID">
        @foreach (var item in Model.allConfirmationDateEmployeeList)
        {
        <tr id="@item.EmployeeID" class="highlightRed">
            <td align="center">
                @Html.DisplayFor(modelItem => item.EmployeeCode, new { @readonly = "readonly" })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmployeeName, new { @readonly = "readonly" })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DeliveryTeam, new { @readonly = "readonly" })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Designation, new { @readonly = "readonly" })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ConfirmationDate, new { @readonly = "readonly" })
            </td>
            <td align="center">
                @Html.CheckBoxFor(modelItem => item.Checked, new { @class = "chkAllConfDateEmployees", @id = item.EmployeeID, @for = item.AppraisalYearID })
            </td>
        </tr>
        }
    </div>
</table>
}

现在这个视图将在 div 中呈现。 现在在我的孩子部分视图中,我无法获得复选框的更改事件。我想依靠更改事件。

图像显示了具有文本框和搜索按钮的局部视图页面。单击按钮时,我需要在搜索按钮下方的 div 标记中呈现其他局部视图。此视图将具有复选框,并且我的复选框更改事件不起作用在那边。 希望你能明白我想说什么。![在此处输入图片描述][1]

【问题讨论】:

  • 发布您的代码,以及您在控制台中可能遇到的任何错误。
  • @sid D:你能把你的jQuery代码也贴出来吗?
  • @downvoter:对第一次来的人有点仁慈。
  • @sid D: 连接事件时元素是否存在于 DOM 中?
  • 我发现实际上有一个父局部视图将呈现子局部视图的方式。如果您查看其源代码,您将找不到子局部视图的数据。访问子局部视图必须编写子部分视图页面中的 jquery 代码。谢谢大家。

标签: jquery asp.net-mvc asp.net-mvc-partialview


【解决方案1】:

如果您正在执行部分视图,并使用 JQuery 选择器监视更改事件,您可能会遇到两个陷阱之一(注意:如果您不使用 JQuery,这无关紧要

  1. 如果 JQuery 脚本位于 DOM 中的元素之上,它将不会在其上执行。要解决这个问题,您应该将所有 JQuery 选择器(例如 $('input').on('change'...) 放在 document.ready 事件中。($(document).ready(function() { ... your jquery here ...});
  2. 如果您的部分视图是通过 AJAX 呈现的,您将需要在其中重新实例化任何 JQuery 选择器。它们在文档就绪事件之后加载

如果这有帮助,请告诉我。

【讨论】:

  • 如何重新实例化任何 JQuery 选择器?
  • 如何重新实例化? @LearningPal
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多