Jquery 使用ajax响应数据追加表行
我正在mvc应用程序中进行一个ajax调用,然后希望向现有表中添加行。我已在下面附上相关代码。 我有一个目录项表,当用户单击一行时,我执行一个ajax调用,该调用成功并返回预期的数据 当我循环遍历数据时,我看到正在创建表行,但是.append()实际上并没有将新行附加到#tblApplications表中 下面的代码有什么问题Jquery 使用ajax响应数据追加表行,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我正在mvc应用程序中进行一个ajax调用,然后希望向现有表中添加行。我已在下面附上相关代码。 我有一个目录项表,当用户单击一行时,我执行一个ajax调用,该调用成功并返回预期的数据 当我循环遍历数据时,我看到正在创建表行,但是.append()实际上并没有将新行附加到#tblApplications表中 下面的代码有什么问题 <table id="tblApplications"> <tr> <th> @Html.DisplayNam
<table id="tblApplications">
<tr>
<th>
@Html.DisplayNameFor(model => model.Partnumber)
</th>
<th>
@Html.DisplayNameFor(model => model.VehicleYear)
</th>
<th>
@Html.DisplayNameFor(model => model.Make)
</th>
<th>
@Html.DisplayNameFor(model => model.ModelDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.EngineDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.Parttype)
</th>
<th>
@Html.DisplayNameFor(model => model.Notes)
</th>
</tr>
@DisplayNameFor(model=>model.Partnumber)
@Html.DisplayNameFor(model=>model.VehicleYear)
@DisplayNameFor(model=>model.Make)
@DisplayNameFor(model=>model.ModelDescription)
@DisplayNameFor(model=>model.EngineDescription)
@DisplayNameFor(model=>model.Parttype)
@DisplayNameFor(model=>model.Notes)
jQuery(函数($){
$('.toggle')。单击(函数(e){
e、 预防默认值();
var$cur=$(this).closest('tr').next('view details').stop().toggle();
$('.view details').not($cur.stop().hide();
var partnum=$(this).closest('tr').find('.PartNumber').text().trim();
$(“#应用程序零件号”).val(零件号);
$.ajax({
url:“@url.Action”(“GetApplications”,“Admin”)”,
数据:{'ApplicationPartNumber':partnum},
成功:功能(响应、状态、xhr){
var apps=JSON.stringify(响应);
var trHTML='';
$.each(JSON.parse(apps)、函数(idx、obj){
trHTML=''+obj.Partnumber+''
+obj.VehicleYear+“”
+对象生成+“”
+obj.ModelDescription+“”
+obj.EngineDescription+“”
+对象零件类型+“”
+对象注释+“”;
$('tblApplications').append(trHTML);
});
},
});
})
})
将响应字符串化是没有意义的。尝试将
数据类型:'json'
添加到$.ajax
选项中,然后只需执行$(响应…
。除此之外..追加过程似乎很合理。我想我找到了问题所在。当在另一个表中单击该行时,TbalApplications嵌套在该表中,ajax被调用以填充TbalApplications。它是用css隐藏的。我如何克服该问题?我了解数据类型:'json'。ThanksUseshow()
在任何隐藏的父对象上是的,没错。谢谢!有一段时间没有使用JavaScript了。这就成功了。另外,在ajax调用中使用数据类型“json”更干净了。
<script>
jQuery(function ($) {
$('.toggle').click(function (e) {
e.preventDefault();
var $cur = $(this).closest('tr').next('.view-details').stop().toggle();
$('.view-details').not($cur).stop().hide();
var partnum = $(this).closest('tr').find('.PartNumber').text().trim();
$("#ApplicationPartNumber").val(partnum);
$.ajax({
url: "@Url.Action("GetApplications", "Admin")",
data: { 'ApplicationPartNumber': partnum },
success: function (response, status, xhr) {
var apps = JSON.stringify(response);
var trHTML = '';
$.each(JSON.parse(apps), function (idx, obj) {
trHTML= '<tr><td>' + obj.Partnumber + '</td><td>'
+ obj.VehicleYear + "</td><td>"
+ obj.Make + "</td><td>"
+ obj.ModelDescription + "</td><td>"
+ obj.EngineDescription + "</td><td>"
+ obj.Parttype + "</td><td>"
+ obj.Notes + "</td></tr>";
$('#tblApplications').append(trHTML);
});
},
});
})
})