简体   繁体   English

在提交过程中使用同步确认灯箱提交表单

[英]Submit form with synchronous confirm lightbox during submission

Problem We need to give facility on lightbox to cancel the form submission or provide extra information to continue with form submissions. 问题我们需要在灯箱上提供便利,以取消表单提交或提供其他信息以继续表单提交。

Detailed Requirements I've a HTML form. 详细要求我有一个HTML表单。 Before form is submitted, we need to show a lightbox, to ask some extra information from the user. 在提交表单之前,我们需要显示一个灯箱,以向用户询问一些其他信息。 In simple words, I need synchronous behaviour in a Javascript function. 简而言之,我需要Javascript函数中的同步行为。 I know it is not expected JS behavior but I'm also sure it is possible and someone must had achieved it by one way or other. 我知道这不是预期的JS行为,但我也确信这是可能的,并且有人必须通过某种方式实现它。 How can we do that. 我们该怎么做。

Code looks like following, hope is clears the problem better. 代码如下所示,希望是更好地解决问题。

//This is called when submit button is clicked.
$('.mbutton').bind('click', function(e){
    var subButton = e.target || e.srcElement || e.originalTarget;
    var value = subButton.id;

    //If submit button pressed.
    if (value == "Publish"){
        //custom JQuery plugin to open light box.
        $.siddlb('#puboptions',{});
        //Lightbox opens but soon form too submits and new page gets loaded. Requirements are, function must wait here until light box is closed.
    }

    //Below task should execute only if user provide required information in lightbox.
    finalize();
    try{
        if (value == "Publish"){
            var canvas = document.getElementById("imageView");
            var context = canvas.getContext("2d");
            var img     = canvas.toDataURL("image/png");
            document.getElementById("textimage").value = img;            
        }
        //$("#myaction").val(subButton.id);
        $("#myaction").val('Publish');
    }catch(e){
        $("#myaction").val("Cancel");                   
    }
});

I guess your publish button (as name suggest) is form's submit button. 我猜您的发布按钮(如名称所示)是表单的提交按钮。

There might be different ways but if I were you, I would have make publish button as simple button like 可能会有不同的方式,但是如果我是你,我会将发布按钮设为简单的按钮,例如

<button class="mbutton" id="publish">Publish</button>

On its click, do whatever you want to do in the lightbox. 点击它,在灯箱中执行任何您想做的事情。 Just place another button in lightbox like 'continue' and on its click, submit the form through javascript. 只需在灯箱中放置另一个按钮(如“继续”),然后单击它,即可通过javascript提交表单。

This is very simple and straight forward solution, may be you are looking for some other solution. 这是非常简单直接的解决方案,可能是您正在寻找其他解决方案。 Please let me know if you find other solution. 如果您找到其他解决方案,请告诉我。

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

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