在ASP.NET MVC中使用jquery从Ajax加载的子视图访问父视图的DOM元素
到目前为止,我从未遇到过从子视图访问父视图的DOM元素的问题。但这是我第一次通过ajax在部分视图中实际加载,在这种情况下,我似乎无法使用JQuery访问父视图的任何元素 这是一个问题,因为子视图实际上只是一个选择表,应该更新父视图,然后再次消失。我可以用一个选择框来做这件事,但考虑到一个非常冗长的列表,我认为这是非常不理想的。另外,我不想在不被使用的情况下提前发送一半的数据库 我做了一些搜索,对于Iframe,window.parent.document似乎是一个答案,但这不是Iframe。坦率地说,我不太确定问题是什么,我没想到脚本会在另一个范围内出现问题。但我似乎无法从父视图访问任何内容,甚至无法访问其他脚本。如何访问父视图的DOM 这是父视图,它本身已经是一个局部视图,但不是由ajax加载的,它包含了我感兴趣的所有元素在ASP.NET MVC中使用jquery从Ajax加载的子视图访问父视图的DOM元素,jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,到目前为止,我从未遇到过从子视图访问父视图的DOM元素的问题。但这是我第一次通过ajax在部分视图中实际加载,在这种情况下,我似乎无法使用JQuery访问父视图的任何元素 这是一个问题,因为子视图实际上只是一个选择表,应该更新父视图,然后再次消失。我可以用一个选择框来做这件事,但考虑到一个非常冗长的列表,我认为这是非常不理想的。另外,我不想在不被使用的情况下提前发送一半的数据库 我做了一些搜索,对于Iframe,window.parent.document似乎是一个答案,但这不是Iframe。坦
<script>
//register events for choose and create button
$(document).ready(function ()
{
$("#choosecustomerbtn").click(function (e)
{
$.ajax({
url: '@Url.Action("_CustomersTable", "Camera")',
success: function (data)
{
$('#customer_table_cont').html(data);
}
});
});
});
</script>
<div class="col-md-12">
<div class="col-md-7">
<div class="col-md-2">
<p>@Html.DisplayNameFor(model => model.Company)</p>
</div>
<div class="col-md-10" id="customer_company_label">
<p>@Html.DisplayFor(model => model.Company)</p>
</div>
<div class="col-md-2">
<p>@Html.DisplayNameFor(model => model.Honorific):</p>
</div>
<div class="col-md-10" id="customer_honorific_label">
<p>@Html.DisplayFor(model => model.Honorific)</p>
</div>
<div class="clearfix"></div>
<div class="col-md-2">
<p>@Html.DisplayNameFor(model => model.Email):</p>
</div>
<div class="col-md-10" id="customer_email_label">
<p>@Html.DisplayFor(model => model.Email)</p>
</div>
</div>
<div class="col-md-5">
<div class="col-md-3 btn btn-default" id="choosecustomerbtn">
Kunde wählen
</div>
</div>
<div class="clearfix"></div>
<!--div that will be used to render the customer table if it is needed-->
<div id="customers_table_cont"></div>
</div>
基本上,它只列出了一个客户、一个公司、一个电子邮件地址和要联系的人。这是一个更广泛的结构的一部分,不涉及手头的问题。我想做的就是,如果有人必须指定另一个客户,他们可以单击choosecustomerbtn并获得一个表格,从中可以轻松地选择新客户并更新标签。因此,单击按钮时,将加载此视图:
@model IEnumerable<qcic_frontend.ViewModels.CustomerViewModel>
@{
Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml";
}
<script>
//register click event for table
$(document).ready(function ()
{
//don't register table header (first row)
isheader = true;
$('tr').each(function ()
{
if (!isheader)
{
$(this).click(function (e)
{
//replace the labels in the customer view
$('#customer_company_label').html($(this).children('.customer_company').text());
$('#customer_honorific_label').html($(this).children('.customer_honorific').text());
$('#customer_email_label').html($(this).children('.customer_email').text());
//remove the table again for better oversight
// $('#customers_table_cont').html("");
});
}
isheader = false;
});
</script>
<table class="table table-hover ajax_table" id="customers_table">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.Company)</th>
<th>@Html.DisplayNameFor(model => model.Honorific)</th>
<th>@Html.DisplayNameFor(model => model.Email)</th>
</tr>
</thead>
<tbody>
@foreach(var i in Model)
{
<tr id="@i.ID">
<td class="customer_company">@Html.DisplayFor(item => i.Company)</td>
<td class="customer_honorific">@Html.DisplayFor(item => i.Honorific)</td>
<td class="customer_email">@Html.DisplayFor(item => i.Email)</td>
</tr>
}
</tbody>
</table>
因此,当单击一个表行时,标签应该会更新,表会再次消失。为什么不共享一些代码?我想可能只是我正在处理的一些概念问题,而我所做的尝试和错误导致代码有点混乱,但如果有帮助的话,我会马上把它放上去。脚本永远不应该放在局部视图中。将它们放在布局的主视图中。