jQuery 对话框不适用于 HTML 表单

发布于 2024-12-27 01:23:09 字数 1762 浏览 0 评论 0原文

我有一个简单的用户帐户管理页面,它允许管理员删除用户帐户。

我希望当管理员用户单击“删除”按钮要求确认时弹出并停止 jQuery UI 对话框。

jQuery 代码:

function getDeleteConfirmation(){
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog-confirm-delete" ).dialog({
        modal: true,
        buttons: {
            "Delete": function() {
                      return true;
                      $( this ).dialog( "close" );          
            },
            Cancel: function() {
                      return false;
                      $( this ).dialog( "close" );  
            }
        }

    });}

PHP 代码:

print "<form action='admin_index.php' method=post>";
print "<input value=".$user_list[$i]."><input type=submit onclick='return getDeleteConfirmation()' value=delete>";
print "</form>";

问题是 jQuery 对话框窗口确实弹出,但它没有停止并等待用户反应,而是很快消失了。页面被重定向并且用户帐户被删除。

然后我尝试更改代码,如下所示;

function getDeleteConfirmation(){
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog-confirm-delete" ).dialog({
        modal: true,
        buttons: {
               "Delete": function() {
                     window.location = 'admin_index.php';   
                     $( this ).dialog( "close" );
            },
                Cancel: function() {
                     $( this ).dialog( "close" );   
            }
        }
    });

删除 HTML 中的表单标签,仅保留输入标签,

print "<input value=".$user_list[$i]."><input type=submit onclick='return getDeleteConfirmation()' value=delete>";

jQuery UI 对话框窗口现在可以停止,但无论我单击“删除”还是“取消”,相应的用户帐户都不会被删除。这似乎是由于 php 变量没有传递引起的。

我只是想知道如何使整个事情正常进行。

希望我已经弄清楚了我的问题,并感谢任何帮助!

I have a simple user account mgmt page, which allows the admin to delete user accounts.

I want a jQuery UI dialog to popup and halt when the admin user clicks on the "delete" buttton asking for confirmation.

jQuery code:

function getDeleteConfirmation(){
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog-confirm-delete" ).dialog({
        modal: true,
        buttons: {
            "Delete": function() {
                      return true;
                      $( this ).dialog( "close" );          
            },
            Cancel: function() {
                      return false;
                      $( this ).dialog( "close" );  
            }
        }

    });}

PHP code:

print "<form action='admin_index.php' method=post>";
print "<input value=".$user_list[$i]."><input type=submit onclick='return getDeleteConfirmation()' value=delete>";
print "</form>";

The problem is that the jQuery dialog window did popup, but instead of halting and waiting for user to react, it soon disappeared. The page got redirected and the user account got deleted.

I then tried to change the code like the follows;

function getDeleteConfirmation(){
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog-confirm-delete" ).dialog({
        modal: true,
        buttons: {
               "Delete": function() {
                     window.location = 'admin_index.php';   
                     $( this ).dialog( "close" );
            },
                Cancel: function() {
                     $( this ).dialog( "close" );   
            }
        }
    });

}

and get rid of the form tags in HTML, leave only the input tags

print "<input value=".$user_list[$i]."><input type=submit onclick='return getDeleteConfirmation()' value=delete>";

the jQuery UI dialog window can now halt but whether I click on "Delete" or "cancel", the corresponding user account didn't get deleted. It seems to be caused by the php variable didn't get passed.

I just would like to know how to make this whole thing work properly.

Hope I have made my problem clear and any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2025-01-03 01:23:09

您的问题是 getDeleteConfirmation 将在对话框执行任何操作之前返回。事情是这样发生的:

  1. 点击提交按钮。
  2. 调用getDeleteConfirmation
  3. 对话框已创建。
  4. getDeleteConfirmation 返回。
  5. 由于 getDeleteConfirmation 未返回 false,因此会触发标准提交按钮行为。
  6. 表格已提交。

您需要在getDeleteConfirmationreturn false来停止提交。然后您需要删除按钮来提交表单。首先,您需要告诉 getDeleteConfirmation 使用什么表单,然后您可以通过按钮进行购买:

<input type="submit" onclick="return getDeleteConfirmation(this)">

然后对 getDeleteConfirmation 进行一些调整:

function getDeleteConfirmation(button) {
    $("#dialog-confirm-delete").dialog({
        modal: true,
        buttons: {
            "Delete": function() {
                $(this).dialog("close");
                $(button).closest('form').submit(); // <--- Trigger the submit.
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }

    });
    return false;  // <--- Stop the submit.
}

演示(使用非功能表单,以便您提交时会出现错误): http://jsfiddle.net/ambiguously/kGSCk/

Your problem is that getDeleteConfirmation will return before the dialog can do anything. Things happen like this:

  1. Hit the submit button.
  2. getDeleteConfirmation is called.
  3. The dialog is created.
  4. getDeleteConfirmation returns.
  5. Since getDeleteConfirmation didn't return false, the standard submit button behavior is triggered.
  6. The form is submitted.

You need to return false in getDeleteConfirmation to stop the submission. Then you need the Delete button to submit the form. First you need to tell getDeleteConfirmation what form to use and you can do that buy giving it the button:

<input type="submit" onclick="return getDeleteConfirmation(this)">

Then some adjustments to getDeleteConfirmation:

function getDeleteConfirmation(button) {
    $("#dialog-confirm-delete").dialog({
        modal: true,
        buttons: {
            "Delete": function() {
                $(this).dialog("close");
                $(button).closest('form').submit(); // <--- Trigger the submit.
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }

    });
    return false;  // <--- Stop the submit.
}

Demo (with non-functioning form so you'll get an error when you submit it): http://jsfiddle.net/ambiguous/kGSCk/

远昼 2025-01-03 01:23:09

JavaScript 不可能等待某事发生。您可以使用确认,否则您必须以不同的方式提交页面。您可以通过在 getDeleteConfirmation 中返回 false 并在对话框中的表单上调用 submit() 来取消单击事件。

或者您可以使用 Ajax 将表单发布到服务器。

此外,您的对话框代码也有无法访问的代码。 return语句之后的代码不可能运行。

It is impossible for JavaScript to wait for something to happen. Either you can use confirm or you will have to submit the page in a different manner. You can cancel the click event by returning false in the getDeleteConfirmation and call submit() on the form in the dialog.

OR you can use Ajax to post the form to the server.

Also your dialog code has unreachable code. It is impossible for the code after the return statements to run.

拒绝两难 2025-01-03 01:23:09

我认为 jQuery UI 对于如此简单的事情来说有点过分了。您是否想过将其放在链接的点击上:

if (confirm("Are you sure you want to contiune with this request?")) {
     //Do stuff
}

编辑:
或者

你可以这样做。简而言之,我们创建了 2 个盒子。第一个带有链接,第二个是弹出框。在弹出框中我们放置链接。一个是删除路径的链接,另一个将隐藏新的弹出窗口。简单。您需要对弹出框进行样式设置,使其性感可爱:)

css:

.deleteSexyBox {
    display: none;
}

html:

//This box is just the simple link
     <div class="delete">Delete</div>
// deleteSexyBox style this up to be sexy however you want. i suggest position: fixed to keep it in the middle of the screen
<div class="deleteSexyBox">
    <a href="linkToWhereEverYouDoTheDeleting">Delete me pleaaaseeee</a>
    <div>Cancel</div>
</div>

根据您的需要设置删除样式。现在对于 JS

$('.delete').click(function(){
     $('.deleteSexyBox').fadeIn();
});
$('.deleteSexyBox div').click(function(){
     $('.deleteSexyBox').fadeOut();
});

如果有多个,那么 js 选择器将需要更多的工作,我假设会有;)
我不确定这是否有帮助。所以如果不是的话请打招呼

I think jQuery UI is overkill for something so simple. Have you thought putting this on the click of your link:

if (confirm("Are you sure you want to contiune with this request?")) {
     //Do stuff
}

Edit:
Or

You can do it this way. In short, we create 2 boxes. One with the link in and the second which is the popup box. Inside the popup box we put the links. One is the link to the delete path the other will hide the new pop-up window. Simples. You'll want to style the popup box to make it sexy and lovely :)

css:

.deleteSexyBox {
    display: none;
}

html:

//This box is just the simple link
     <div class="delete">Delete</div>
// deleteSexyBox style this up to be sexy however you want. i suggest position: fixed to keep it in the middle of the screen
<div class="deleteSexyBox">
    <a href="linkToWhereEverYouDoTheDeleting">Delete me pleaaaseeee</a>
    <div>Cancel</div>
</div>

Style the delete however you want. Now for the JS

$('.delete').click(function(){
     $('.deleteSexyBox').fadeIn();
});
$('.deleteSexyBox div').click(function(){
     $('.deleteSexyBox').fadeOut();
});

The js selectors will need more work if there are to be more than one, which i assume there will be ;)
I'm not sure if this will help. So holla me if its not

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文