我在点击#show-delete时加载了一个模态。在该模态(这是一个远程页面)中,我想附加一个处理程序,点击#delete-user。这个处理程序根本不工作,按钮什么都不做。
如果我将deleteAccount函数放在实际的模态页面(m_delete_user.php)中,则处理程序可以正常工作,但为了方便起见和其他操作,我想将它们保存在index.php中。
在index.php里面:
// show delete user modal
var showDelete = function() {
$('#show-delete').on('click', function () {
var $form = $(this).closest('form');
var username = $form.find("select[name=account_username]").val();
//open the modal with the username value
$('#modal-ajax').load('/spc_admin/modals/m_delete_user.php?username='+username);
$('#modal-ajax').modal('show');
// initialize the button handler
deleteAccount();
});
}
// delete user (modal delete button)
var deleteAccount = function() {
$('#delete-user').on('click', function () {
var $form = $(this).closest('form');
$.ajax({
type: 'post',
url: '/spc_admin/process/p_delete_user.php',
data: $form.serialize(),
dataType : 'json'
}).done(function (response) {
if (response.success) {
... and continued
答案 0 :(得分:1)
您必须等到.load()
操作完成,因为它是异步的。您可以使用传递给.load()
的完成函数来执行此操作,并使用完成函数触发deleteAccount处理程序的安装。
// delete user (modal delete button)
var deleteAccount = function() {
$('#delete-user').on('click', function () {
var $form = $(this).closest('form');
$.ajax({
type: 'post',
url: '/spc_admin/process/p_delete_user.php',
data: $form.serialize(),
dataType : 'json'
}).done(function (response) {
if (response.success) {
... and continued
// show delete user modal
var showDelete = function() {
$('#show-delete').on('click', function () {
var $form = $(this).closest('form');
var username = $form.find("select[name=account_username]").val();
//open the modal with the username value
// ==> deleteAccount added as a completion function here
$('#modal-ajax').load('/spc_admin/modals/m_delete_user.php?username='+username, deleteAccount);
$('#modal-ajax').modal('show');
});
}