0

学习 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'
    });
}

我在哪里错了?

4

0 回答 0