jquery-ui 대화 상자에서 열리고 ajax를 통해 제출되고 json 형식으로 업데이트 된 데이터를 읽고 페이지를 업데이트하는 양식이 있습니다. 처음에는 모든 것이 잘 작동합니다. 후속 실행에서 데이터는 데이터베이스에서 올바르게 업데이트되지만 페이지의 업데이트는 첫 번째 실행과 동일한 셀에 적용됩니다.후속 사용에 대한 ajax 업데이트 관련 문제
웹 개발자 도구 모음에서 "생성 된 소스보기"를 사용하면 이전 양식이 아직 DOM에 있음을 알 수 있습니다. 따라서이 $("form#hostEdit").find("input#id").val()
은 항상 첫 번째 양식을 찾거나 한 번만 평가됩니다. jQuery에 익숙하지 않아서 어떻게해야할지 모르겠습니다. 그 양식은 여전히 DOM에 있어야합니까? 다른 선택기를 사용해야합니까? 뭔가 다른 거? 업데이트를 적용
$("table.hostgrid a.new").click(function() {
var id = this.id.substring(3);
var $dialog = $('<div></div>')
.load('/hosts/new?byte1=${network.byte1}&byte2=${network.byte2}&byte3=${network.byte3}&byte4=' + id);
var getHostAction = #{jsAction @NetworkHosts.view(':id') /}
$dialog.dialog({
autoOpen: false,
title: 'New host at ${network.byte1}.${network.byte2}.${network.byte3}.' + id,
width: 500,
buttons: {
"Create": function() {
$.ajax({
async: true,
type: 'post',
url: '/hosts/create',
data: $("form#hostNew").serialize(),
success: function(response) {
$dialog.html(response);
if ($("div.flashSuccess") != null) {
$dialog.dialog('destroy');
$.ajax({
url: getHostAction({'id': $("form#hostEdit").find("input#id").val()}),
dataType: 'json',
success: function(data) {
updateHost(data);
},
error: function(data, msg, exception) {
alert("Error during request: " + msg);
},
});
}
}
});
}
}
});
$dialog.dialog('open');
return false;
});
기능 : 당신 updateHost() 함수는, 잘못 무슨 일이 일어나고 있는지 결정하기 위해 정말 열심히 될 것입니다 무엇을 보지 않고
function updateHost(host) {
var cell = $("td#dot"+host.byte4);
cell.fadeOut("fast", function() {
cell.find("span.hostname").text(host.hostname).attr("title", host.description);
cell.removeClass().addClass(host.agegroup);
if (host.type)
cell.addClass(host.type.toLowerCase());
if (host.is_dhcp)
cell.addClass("dhcp");
if (host.is_service)
cell.addClass("service");
cell.fadeIn("fast");
});
}
들여 쓰기 크기를 위해 두 개의 공백 만 사용하도록 코드를 다시 포맷 할 수 있습니다. –
@Chris - 그것은 약간의 이유로 약간 과장되었습니다. –
업데이트 된 $ ("form # hostEdit"). find ("input # id") 값? – ifaour