将表单脚本从jquery 1.4转换为1.9

Convert a form script from jquery 1.4 to 1.9

本文关键字:转换 jquery 表单 脚本      更新时间:2023-09-26

我想转换一个脚本,我发现…但是我不能。它与Jquery 1.5一起工作。但它不再工作与Jquery 1.6

但是我尝试了jquery -migrate(将旧的jquery代码迁移到jquery 1.9+)。但是没有结果……

脚本:

$.post('', form.serialize(), function(msg) {
        submitFlag = false;
        overlay.hide();
        $('span.errorIcon').remove();
        if (msg.success) {
            $('#formContainer, #mdiv, #contactboxfloat').fadeOut(function() {
                form.get(0).reset();
                $('#thankYou').fadeIn();
            });
        } else {
            $.each(msg, function(k, v) {
                var errorIcon = $('<span>', {
                    className: 'errorIcon'
                });
                var errorTip = $('<span>', {
                    className: 'errorTip',
                    text: v
                }).hide().appendTo(errorIcon);
                errorIcon.hover(function() {
                    errorTip.stop().fadeIn(function() {
                        errorTip.css('opacity', 1);
                    });
                }, function() {
                    errorTip.stop().fadeOut('slow', function() {
                        errorTip.hide().css('opacity', 1);
                    });
                });
                form.find('[name=' + k + ']').closest('.formRow').append(errorIcon);
                if ($(window).width() - errorIcon.offset().left > 240) {
                    errorTip.css('left', 30);
                } else {
                    errorTip.css('right', 30);
                }
            });
        }
    }, 'json');
php json

    if (!function_exists('json_encode'))
{
    function json_encode($a=false)
    {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
            if (is_float($a))
            {
                // Always use "." for floats.
                return floatval(str_replace(",", ".", strval($a)));
            }
            if (is_string($a))
            {
                static $jsonReplaces = array(array("''", "/", "'n", "'t", "'r", "'b", "'f", '"'), array('''''', '''/', '''n', '''t', '''r', '''b', '''f', ''"'));
                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
            }
            else return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
            if (key($a) !== $i)
            {
                $isList = false;
                break;
            }
        }
        $result = array();
        if ($isList)
        {
            foreach ($a as $v) $result[] = json_encode($v);
            return '[' . join(',', $result) . ']';
        }
        else
        {
            foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
            return '{' . join(',', $result) . '}';
        }
    }
} 

有人能告诉我是什么阻止了脚本的工作吗?

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    try{
        $contactForm->validate();
        $contactForm->send();
        $thankYou = $config['thankYouMessage'];
        if(IS_AJAX){
            echo json_encode(array('success'=>1));
            exit;
        }
    }
    catch(FormValidateException $e){
        if(IS_AJAX){
            echo json_encode($e->errors);
            exit;
        }
        else{
            $contactForm->populateValuesFromArray($_POST);
        }
    }
    catch(Exception $e){
        die('{"exception":"'.$e->getMessage().'"}');
    }
}

有两件事会阻碍JQuery的向后兼容性:

  • $()不再与$(document)相同,它是一个空的jQuery对象
  • jQuery将只解析合法的JSON,而不是那些看起来像JSON但技术上不合法的东西

检查JSON是否有效

来源:https://forum.jquery.com/topic/jquery-1-8-1-and-jquery-1-4-backward-compatible-with-existing-jquery-1-3-2-custom-code