简体   繁体   English

表单提交上的jQueryUI模式确认对话框

[英]jQueryUI Modal confirmation dialog on form submission

I am trying to get a modal confirmation dialog working when a user submits a form. 我想在用户提交表单时尝试使用模态确认对话框。 My approach, I thought logically, would be to catch the form submission. 我的逻辑思路是,我的方法是抓住表单提交。 My code is as follows, 我的代码如下,

$('#multi-dialog-confirm').dialog({
    autoOpen: false,
    height: 200,
    modal: true,
    resizable: false,
    buttons: {
        'Confirm': function(){
            //$(this).dialog('close');
            return true;
        },
        'Cancel': function(){
            $(this).dialog('close');
            return false;
        }
    }
});

$('#completeform').submit(function(e){
    e.preventDefault();
    var n = $('#completeform input:checked').length;

    if(n == 0){
        alert("Please check the item and mark as complete");
        return false;
    }else{
        var q = $('#completeform #qty').html();
        if(q > 1){
            $('#multi-dialog-confirm').dialog('open');
        }
    }
    //return false;
});

So I'm setting up my dialog first. 所以我先设置对话框。 This is because I'm pretty certain that the scope of the dialog needs to be at the same level as the function which calls it. 这是因为我非常肯定对话框的范围需要与调用它的函数处于同一级别。

However, the issue is that when you click 'Confirm' nothing happens. 但问题是,当您单击“确认”时,没有任何反应。 The submit action does not continue. 提交操作不会继续。 I've tried $('#completeform').submit(); 我试过$('#completeform').submit(); also, which doesn't seem to work. 此外,这似乎不起作用。 I have tried removing the .preventDefault() to ensure that the form submission isn't completely cancelled, but it doesn't seem to make a difference between that and returning false. 我已经尝试删除.preventDefault()以确保表单提交没有完全取消,但它似乎没有区别并返回false。

Not checking the box, show and alert fine. 不选中此框,显示并提醒。 Might change to dialog at some point ;), clicking 'Cancel' closes the dialog and remains on the page, but the elusive 'Confirm' buttons seems to not continue with the form submission event. 可能会在某些时候更改为对话框;),单击“取消”关闭对话框并保留在页面上,但难以捉摸的“确认”按钮似乎不会继续表单提交事件。

If anyone can help, I'd happily share my lunch with you! 如果有人可以提供帮助,我很高兴与您分享我的午餐! ;) ;)

When doing .submit in the dialogue, the submit code is executed once again. 在对话框中执行.submit时,将再次执行提交代码。

Try this: 试试这个:

$('#multi-dialog-confirm').dialog({
    autoOpen: false,
    height: 200,
    modal: true,
    resizable: false,
    buttons: {
        'Confirm': function(){
            dialogueIsSubmitting = true;
            $('#completeform').submit();
            return false;
        },
        'Cancel': function(){
            dialogueOpen = false;
            $(this).dialog('close');
            return false;
        }
    }
});

var dialogueIsSubmitting = false;

$('#completeform').submit(function(e){
    if(dialogueIsSubmitting) return true;

    var n = $('#completeform input:checked').length;

    if(n == 0){
        alert("Please check the item and mark as complete");
        return false;
    }else{
        var q = $('#completeform #qty').html();
        if(q > 1){
            $('#multi-dialog-confirm').dialog('open');
        }
    }
    return false;
});

Figured I should include my code here, in case someone finds it helpfull. 想我应该在这里包含我的代码,以防有人发现它有用。

    /*
 * Setup the confirm multiple completions dialog
 */
$('#multi-dialog-confirm').dialog({
    autoOpen: false,
    height: 200,
    modal: true,
    resizable: false,
    buttons: {
        'Confirm': function(){
            $(this).dialog('close');
            document.completeform.submit();
        },
        'Cancel': function(){
            $(this).dialog('close');
            return false;
        }
    }
});

/*
 * When completing an item on the search page, validate and confirm
 */
$('#completeform').submit(function(e){
    var n = $('#completeform input:checked').length;

    if(n == 0){
        alert("Please check the item and mark as complete");
        return false;
    }else{
        var q = $('#completeform #qty').html();
        if(q > 1){
            e.preventDefault();
            $('#multi-dialog-confirm').dialog('open');
        }else{
            return true;
        }
    }
    //return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM