多次调用ajax脚本无法正常工作

时间:2022-12-06 10:35:37

OK so I am very new (literally less than a few days) to all things ajax, but it is required for a form I am building for my employer.

好的,所以我对ajax的所有东西都很新(字面上不到几天),但它是我为我的雇主建造的表格所必需的。

Basically, no matter what I do it just will not work for more than 1 function. To elaborate, I am trying to update 4 different parts of a page based on one drop down using "onchange". Now updating 1 part works fine, updating any more than that fails...but not only fails, it also fails if for example I do call to ajax part, then just a simple alert...but if I do it with the alert first it works, then falls over again if I put anything after the ajax call. I hope that makes sense. It also works if I do, for example. onchange, and onblur on the same element, it will execute twice. I'll post the code then hopefully it will make more sense instead of me babbling lol.

基本上,无论我做什么,它都不会超过1个功能。详细说明,我试图使用“onchange”基于一个下拉列表更新页面的4个不同部分。现在更新1部分工作正常,更新任何更多失败...但不仅失败,它也失败,例如我打电话给ajax部分,然后只是一个简单的警报......但如果我这样做与警报首先它工作,然后如果我在ajax调用后放任何东西再次摔倒。我希望这是有道理的。例如,如果我这样做,它也可以工作。 onchange和onblur在同一个元素上,它将执行两次。我会发布代码,然后希望它会更有意义,而不是我bab呀学语。

<select name="pType" id="ptype" onchange="dostuff()">

So that's the input element...

这就是输入元素......

<script type="text/javascript">
function dostuff(){
ajaxpage('adminincludes/popoptions.php?pID=<?= $sql['pID']; ?>&pType=' + ptype.value,'options');
alert('test');
}
</script>

...and thats the dostuff code, or an example anyway, ignore the php part as it fails regardless of that, that part works fine.

...那就是dostuff代码,或者一个例子,无论如何都忽略了php部分,因为它无论如何都会失败,那部分工作正常。

now the rest of the code is within an external file and i believe that somewhere in there is where the problem lies...however I am new to ajax, and am not the greatest with js as I have never really had a major need for it so just learned what I needed, when i needed.

现在其余的代码都在一个外部文件中,我相信在那里的某个地方存在问题所在...但是我是ajax的新手,并且我不是最好的js,因为我从来没有真正需要它只是在我需要的时候才知道我需要什么。

    var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects = ""
var rootdomain = "http://" + window.location.hostname
var bustcacheparameter = ""

function ajaxpage(url, containerid) {
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
    page_request = new XMLHttpRequest()
    else if (window.ActiveXObject) { // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e) {
            try {
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e) {}
        }
    } else
    return false
    page_request.onreadystatechange = function () {
        loadpage(page_request, containerid)
    }
    if (bustcachevar) //if bust caching of external page
    bustcacheparameter = (url.indexOf("?") != -1) ? "&" + new Date().getTime() : "?" + new Date().getTime()
    page_request.open('GET', url + bustcacheparameter, true)
    page_request.send(null)
    page_request.send(null)
}

function loadpage(page_request, containerid) {
    if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)) document.getElementById(containerid).innerHTML = page_request.responseText
}

function loadobjs() {
    if (!document.getElementById) return
    for (i = 0; i < arguments.length; i++) {
        var file = arguments[i]
        var fileref = ""
        if (loadedobjects.indexOf(file) == -1) { //Check to see if this object has not already been added to page before proceeding
            if (file.indexOf(".js") != -1) { //If object is a js file
                fileref = document.createElement('script')
                fileref.setAttribute("type", "text/javascript");
                fileref.setAttribute("src", file);
            } else if (file.indexOf(".css") != -1) { //If object is a css file
                fileref = document.createElement("link")
                fileref.setAttribute("rel", "stylesheet");
                fileref.setAttribute("type", "text/css");
                fileref.setAttribute("href", file);
            }
        }
        if (fileref != "") {
            document.getElementsByTagName("head").item(0).appendChild(fileref)
            loadedobjects += file + " " //Remember this object as being already added to page
        }
    }
}

Now like I said, the code works perfect when only calling ajaxpage() once, or multiple times via different events, it just will not work multiple times from one event, even when putting the multiple instances into the dostuff() function.

就像我说的那样,代码在仅调用ajaxpage()一次或通过不同事件多次调用时工作得很完美,即使将多个实例放入dostuff()函数,它也不会在一个事件中多次工作。

Any help would be greatly appreciated as this is really starting to aggravate me. Thanks.

任何帮助将不胜感激,因为这真的开始加剧我。谢谢。

UPDATE: This isn't as urgent now as i have done a "workaround" which uses multiple events such as mouseover, mouseout etc on an update link instead. which means it works as i need it to, however it is not elegant by any means and I am still intrigued why it won't work when called multiple times within 1 event?!

更新:现在这并不紧急,因为我做了一个“替代方法”,它在更新链接上使用了鼠标悬停,鼠标输出等多个事件。这意味着它可以像我需要的那样工作,但它无论如何都不优雅,我仍然很感兴趣,为什么它在1次事件中被多次调用时不起作用?!

1 个解决方案

#1


0  

I notice on lines 26 and 27 of your sample you're repeating:

我注意到您重复的样本的第26和27行:

    page_request.send(null)

Worth eliminating that before you continue. Could we see a live link to this anywhere so we can perhaps examine generated source?

值得在继续之前消除它。我们可以在任何地方看到这个实时链接,以便我们可以检查生成的源吗?

#1


0  

I notice on lines 26 and 27 of your sample you're repeating:

我注意到您重复的样本的第26和27行:

    page_request.send(null)

Worth eliminating that before you continue. Could we see a live link to this anywhere so we can perhaps examine generated source?

值得在继续之前消除它。我们可以在任何地方看到这个实时链接,以便我们可以检查生成的源吗?