ReadyState上的Fire函数 - XMLHttpRequest

时间:2013-10-01 18:20:51

标签: php jquery ajax xmlhttprequest

学习AJAX。 我有以下AJAX脚本,我主要用它来导航(后端php):

function ajaxFunction(linked) {
    var ajaxRequest;
    var loading = $('#loading');
    if(loading.length > 0) {
       loading.css('display', 'block');
    }

    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data 
    // sent from the server and will update
    // div section in the same page.
    ajaxRequest.onreadystatechange = function() { **//READY STATE HERE!**
        if(ajaxRequest.readyState == 4) {
            var ajaxDisplay = $('#wrapper');
            loading.css('display', 'none');
            ajaxDisplay.html(ajaxRequest.responseText);
            if($('#search_field').length > 0) {
                $('#search_field').focus();
            }
            if($('#year').length > 0) {
                $('html').scrollTo('#year');
            }
            startValidation(); **// VALIDATION FUNCTION HERE!**
        }
    }
    // Now get the value from user and pass it to
    // server script.
if(linked == 'addNewPage' || linked == 'add') {
    var queryString = "?page=add";
    queryString +=  "&action=customer&add=new&ajax=ajaxRequest";

} else if(){} etc...

我已经下载了h5validate插件以利用我已经拥有的HTML5验证,问题是当我通过AJAX加载内容时。任何.ready函数从DOM中解除绑定并且不起作用。我试过在ReadyState == 4之后调用该函数,但它仍然无法启动。如果我直接导​​航到它(没有AJAX)并使用:

,该函数有效
window.onload = startValidation();

验证触发器:

function startValidation() {
    $('.row').h5Validate({
        errorClass:'red'
    });
}

我在哪里错了?

0 个答案:

没有答案