简体   繁体   English

成功提交表单后,jQuery关闭弹出窗口

[英]jQuery closing pop-up after successful form submit

I have a pop up form that i wish to close on successfull submit of the form. 我有一个弹出表单,希望在成功提交表单后关闭。 Currently we are only validating on the server side which will create an unordered list of validation errors with the class .error_message. 当前,我们仅在服务器端进行验证,这将使用.error_message类创建一个无序的验证错误列表。

Once the form has been successfully submitted i want to close the window. 表单成功提交后,我想关闭窗口。

Currently the jQuery i am using for this is as follows but does not work because the first time the form has been submitted the number of error messages will always be zero. 目前,我为此使用的jQuery如下,但由于第一次提交表单,错误消息的数量始终为零,因此无法正常工作。

$(document).ready(function() {

$('form#emailQuote').submit(function() {

    var $emailErrors = $('form#emailQuote ul.error_message').length;

    alert($emailErrors);

    if ($emailErrors == 0) {
        //window.close();
        alert('no errors');

    } else {
        //alert('errors');
        alert('errors');
    }      
});

}); });

You can do something like this (here server side uses php, but can be anything) : 您可以这样做(在服务器端使用php,但可以是任何东西):

<form onsubmit="javascript:do_submit(); return false;"  method="post" >
.... 
</form>

<script type="text/javascript">
function do_submit()
{
  var params = {"param1":$("elem1").val(), "param2":$("elem2").val(), ....};
  $.ajax
  ({
      url:"ajax_handler.php",
      type:"POST",                      
      data:params,
      success: function(retval)
      {                             
         var err_code;
         if (! $.browser.msie)
         {
           if ($('error_code:first',retval))
           {
             err_code = $('error_code:first',retval).text();   
             //check err_code and show error, or redirect to a different page ,or close window             
           }
           else 
           {
               //bad xml, show error message
           }
         }
         else // we need to load xml differently in case of IE
         {
            var xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            try
            {
               xml.loadXML(retval);
               err_code = xml.documentElement.getElementsByTagName('error_code')[0].text;
               //check err_code and show error, or redirect to a different page ,or close window
             }
             catch (error)
             {
               //bad xml, show error message
             }
         }
      }
    });
}
</scirpt>

Php:
<?php
  $xml = new DOMDocument( "1.0", "UTF-8" );
  $xml_root = $xml->createElement("response");        
  $xml->appendChild($xml_root);    
  $xml_code  = $xml->createElement("error_code");
  $xml_root->appendChild($xml_code);
  // process request 
  // ....
  $xml_val = $xml->createTextNode(utf8_encode(0)); // 
  $xml_code->appendChild($xml_val);

  print($xml->saveXML());
?>

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

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