Django表单在IE中脚本行为不正常

时间:2013-01-09 16:33:41

标签: ajax django jquery django-forms

我有一个django表单,其中包含姓名,地址,邮编等,以及捐赠服务的提货日期字段。我进行了设置,以便在用户输入邮政编码并离开该字段后向django视图发送ajax请求以返回与该邮政编码匹配的所有拾取。在chrome,firefox,safari中一切都运行良好,但当然IE在你知道什么是一个皇家的痛苦。尽管已启用JS并查看源代码,但下面的$('#results').hide();似乎对某些用户不起作用。不知怎的,这一切都破裂了?

错误一直是在IE中没有返回提取日期,并且在提交时它给出了403 csrf错误,我在表单中有标记。下面是我表格的JS。 IE中是否有任何特定的东西会导致问题?获取提货日期的主要功能是函数get_pickup_dates

Query(document).ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    function sameOrigin(url) {
        // url could be relative or scheme relative or absolute
        var host = document.location.host; // host + port
        var protocol = document.location.protocol;
        var sr_origin = '//' + host;
        var origin = protocol + sr_origin;
        // Allow absolute or scheme relative URLs to same origin
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
            // or any other URL that isn't scheme relative or absolute i.e relative.
            !(/^(\/\/|http:|https:).*/.test(url));
    }
    function safeMethod(method) {
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});

function get_pickup_dates(zip,template)
{
    $.ajax({
        type: "POST",
        url:"/get_pickups/",
        data: {
        'zip': zip, 
        'template':template
        },
        success: function(data){            

                date_results = $(data).find('#results').html()          
                $("#id_pickup_date").replaceWith("<span>" + date_results + "</span >");     
        },
        error: function(){
            alert("Error");
        }
    })
}


$(document).ready(function()
{   

template = window.location.pathname;
$('#results').hide();
$('#id_cancel').closest('div').hide()

    if (template == '/donate/')
    {
        address_fields = $("#id_address_number, #id_city, #donateForm #id_zip, #externalForm #id_zip, #id_address_street, #id_pickup_id").closest('div');
        $(address_fields).hide();

    }

    if (template == '/external/')
    {
        $("#id_zip").live('focusout',function()
        {
            zip = $(this).val()
            get_pickup_dates(zip,template)
        });
    }

    if( $("ul").hasClass('errorlist') && template == '/donate/')
    {
        address_fields.show();
    }    

    $("#id_address").live('focusout',function()
    {

        var intTest = /^\d{5}$/;
        $this = $(this);    
        var address = $this.val();      
        var split_addr = address.split(' ');
        var find_street = address.split(',');
        var street = find_street[0];
        var trim_street = street.split(' ');

        var clean_street = trim_street.splice(1,2).join(" ");       

        $('#id_address_number').val(split_addr[0]);

        // Find ZIP code in address
        for(i=0; i <= split_addr.length; i++)
        {           
            if(intTest.test(split_addr[i]))
            {
                $("#id_zip").val(split_addr[i]);
                zip = split_addr[i];
                //alert(zip);               
                break;
            }
        }

        //Find city
        $('#id_city').val($.trim(find_street[1]));


        // Find Street in address
        $("#id_address_street").val(clean_street);
        if(template == '/donate/')
        {   
            $(address_fields).show("slow");
            $('#id_pickup_id').closest('div').hide();
        }

        get_pickup_dates(zip,template)
    });


    $("#id_address").keypress(function (e)
    {
        if (e.which == 13)
        {
          return false;
        }
        return true
    });

    $('div.field input, div.field select, div.field checkbox, div.field textarea').focus(function()
    {
        $this = $(this);        
        $this.closest('div').addClass('highlight');

    });

    $('div.field input, div.field select, div.field checkbox, div.field textarea').blur(function()
    {
        $this = $(this);

        $this.closest('div').removeClass('highlight');

    });

    if ($('#id_zip').val().length > 0)
    {
        zip = $('#id_zip').val()
        get_pickup_dates(zip,template)
    }    
});

1 个答案:

答案 0 :(得分:0)

如果您使用的是iFrame,这可能是jQuery / IE兼容性中已知(现在已修复)的错误。见http://bugs.jquery.com/ticket/13936