dongtao9887 2013-10-01 18:20
浏览 28

ReadyState上的Fire函数 - XMLHttpRequest

Learning AJAX. I have the following AJAX script, i am using it primarily for navigation (backend 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...

I have downloaded the h5validate plugin to take advantage of the HTML5 validation i already have in place, the problem is when i load the content through AJAX any .ready functions get unbound from the DOM and don't work. I have tried calling the function after ReadyState==4 but it still won't launch. The function works if i navigate to it directly (without AJAX) and use:

window.onload = startValidation();

Validate trigger:

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

Where am i going wrong here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集