1

What i'm trying to achieve?

I have a div that is controlled by JS that has its height changed when a menu item is clicked. So when a menu item is clicked a div appears, when it is clicked again it disappears.

Within this div i have a form, when the form is submitted a message will display asking the user if they are sure they want to submit form. If they answer yes, the form submits and an email is sent.

If the user says no the message will disappear allowing the user to make changes.

Problem

When a form is submitted the page refreshes due to the post. Thus making the div disappear.

My solution?

When the user says yes the form is posted using a jQuery.post() along with a e.preventDefault().

This should then

  1. Stop the form from refreshing (thus the div with the form is open still)
  2. Post the form data

Code

     $('#form1').submit(function(e) {
            var currentForm = this;
            e.preventDefault();
             console.log("prevent default1");//Debug Line
            bootbox.confirm("Are you sure?", function(result) {
                if (result) {
                    e.preventDefault();
                    console.log("prevent default2");//Debug Line
                      $.ajax({
               type: 'POST',
               url: 'indextest2.php',
               data: $("#form1").serialize(),
               error: function () {
                   console.log('Ajax Error');//Debug Line
               }, 
               success: function (response) {
                    console.log('Ajax Success'); //Debug Line
                        }
                    });
                }
            }); 
        }); 

What is happening?

The form is still posting causing a refresh when the user says "yes" to the message.

4

2 回答 2

2

我认为你可以return false;$.post调用之后(而不是之后e.preventDefault()(这是为了让 bootbox 等待发布结果)和一个return false;就在bootbox.confirm子句之后(为了不提交表单)。你可以在你的成功方法上调用 bootbox。 hideAll 发布后关闭引导箱。

于 2014-11-03T13:06:10.360 回答
0

请放在return false;后面e.preventDefault();

e.preventDefault();
return false;
于 2014-02-04T19:55:17.327 回答