jQuery 帖子和对话框
我正在使用 $.post() 从数据库中检索内容,然后我想用它来操作表单值。完成此操作后,我希望打开 jQuery 对话框。这是我正在编写的一个非常简单的“编辑事件”系统。我只是无法在 $.post() 内打开对话框,如果我在 $.post() 之外打开对话框,表单值将返回空。
我理解它不起作用的概念,因为无论回调是否成功,脚本都会继续运行,但是还有其他方法可以做到这一点吗?
我的代码:
// Edit a banner:
$("input[name='eventEditBtn']").click(function() {
var eventID = $(this).attr("rel");
$.post("www/scripts/ajax/getEventInfo.php",{id : eventID},function(data) {
if(data.success == true) {
var info = data.info;
$("input[name='editEventName']").val(info.name);
$("input[name='editEventDate']").val(info.date);
$("input[name='editEventTime']").val(info.time);
$("textarea[name='editEventSummary']").val(info.summary);
$("textarea[name='editEventDescription']").val(info.description);
$("#editEventCurrentCategory").html("The current category is: "+info.categoryName);
$("input[name='editEventVenue']").val(info.venue);
$("input[name='editEventCost']").val(info.cost);
$("#editEventCurrentStatus").html("The current status is: "+info.categoryName);
$("#editEventContainer").dialog({width: 600, title: "EDIT EVENT:"});
} else {
$("<div />").dialog("An error has occured retrieving this event information.");
return false;
}
});
return false;
});
I am using $.post() to retrieve content from the database, which I then want to use to manipulate form values. Once this is done I want the jQuery dialog box to open. It is for a very simple "edit event" system I am writing. I just cannot get the dialog box to open within the $.post(), and if I do it outside of the $.post(), the form values return empty.
I understand the concept that it won't work because the script continues to run irrespective if the callback is successful, but is there another way to do this?
My code:
// Edit a banner:
$("input[name='eventEditBtn']").click(function() {
var eventID = $(this).attr("rel");
$.post("www/scripts/ajax/getEventInfo.php",{id : eventID},function(data) {
if(data.success == true) {
var info = data.info;
$("input[name='editEventName']").val(info.name);
$("input[name='editEventDate']").val(info.date);
$("input[name='editEventTime']").val(info.time);
$("textarea[name='editEventSummary']").val(info.summary);
$("textarea[name='editEventDescription']").val(info.description);
$("#editEventCurrentCategory").html("The current category is: "+info.categoryName);
$("input[name='editEventVenue']").val(info.venue);
$("input[name='editEventCost']").val(info.cost);
$("#editEventCurrentStatus").html("The current status is: "+info.categoryName);
$("#editEventContainer").dialog({width: 600, title: "EDIT EVENT:"});
} else {
$("<div />").dialog("An error has occured retrieving this event information.");
return false;
}
});
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是因为在初始化对话框之前正在修改输入?尝试在 $.post 调用之前初始化它,例如:
然后您的 $.post 调用使用以下命令打开对话框:
还要记住检查选择器是否正确。
Maybe because the inputs are being modified before the dialog is being initialized? Try to initialize it before the $.post call like this, for example:
and then your $.post call opens the dialog using:
Remember also to check that the selectors are right.