【发布时间】:2015-03-03 07:46:56
【问题描述】:
我遇到了敲除和 jquery 对话框的问题。问题是我正在打开超链接上的对话框。问题是每次我关闭并打开对话框时,我都会得到重复数据。每当我关闭并打开对话框时,数据就会一次又一次地附加。我尝试在关闭对话框时销毁对话框,但问题仍然存在。
我在下面的代码中做了什么
1) 我正在打开一个带有对话框的 div。在对话框的打开事件中,我正在从服务器获取数据。
2) 我正在使用淘汰视图模型来存储和持久化数据。
问题是什么
每当我打开和关闭对话框时,我都会在每次点击超链接时得到重复的行。该问题不会在第一个页面加载时发生。该问题仅在进一步单击超链接时出现。
<div id="divforjobactivity" class="hidden">
<h5 class="title1" style="padding-left: 500px">
Job Activity Form</h5>
<table id="gvActivityForm" class="test">
<thead>
<tr> <td> <div id="divstatus"></div> </td></tr>
<tr>
<th class="thdata">
Y1
</th>
<th class="thdata">
Y2
</th>
</tr>
</thead>
<tbody data-bind="foreach: arraytoadd">
<tr>
<td data-bind="text: y1"></td>
<td data-bind="text: y2"></td>
</table>
</div>
$(document).on("click", "[id*=hypJobActivity]", function () {
var $tr = $(this).closest('tr'); // gets closest parent tr element to the anchor element
var x1 = $(".x1", $(this).closest("tr")).html()
var $addJobActivityContainer = $("#divforjobactivity");
TemplateFunction = function () {
var self = this;
self.y2 =ko.observable(0);
self.y1 = ko.observable(0);
}
// set the view model for
JobActivity = function () {
var self = this;
self.errors = ko.observableArray();
self.arraytoadd = ko.observableArray();
self.addevent = function () {
self.arraytoadd.push(new TemplateFunction());
}
}
jobactivityVM = new JobActivity();
ko.cleanNode($addJobActivityContainer[0]);
ko.applyBindings(jobactivityVM, $addJobActivityContainer[0]);
obj = {};
obj.x1 = x1;
$("#divforjobactivity").dialog({ height: 320,width: 1230,modal: true,
open:function (event,ui)
$.ajax({ //start ajax call for posting and getting the data back
type: 'POST',
url: 'PopupWebService.asmx/ReceiveandSendJobActivity',
data: JSON.stringify({ item: obj }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
for (var i = 0; i < response.d.length; i++) {
var x = new TemplateFunction();
x.y1(response.d[i].Y1);
x.y2=((response.d[i].Y2);
jobactivityVM.arraytoadd.push(x);
}
}, //end of sucess att
close: function (event, ui) {
$("#divforjobactivity").dialog("destroy");
$("#divforjobactivity").remove();
}, // end of close
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Unable to retrieve requested information " + errorThrown);
} //end of error
}); //END OF DIALOG
【问题讨论】:
-
能否请您发布 HTML。此外,我似乎在上面的示例中找不到 $.ajax 调用的结束“)”
-
罗伯特。我更新了代码
标签: jquery asp.net knockout.js