简体   繁体   English

加载AJAX后调用Javascript函数

[英]Call Javascript function after AJAX load

I know that these kind of questions pop up a lot on here. 我知道这些问题在这里突然出现。 I usually am not the type to ask questions but this time I could not find a solution through searching. 我通常不是那种问问题的人,但是这次我无法通过搜索找到解决方案。

I have boiled down my problem to a very small and simple environment: 我把问题归结为一个非常简单的环境:

I have 3 files: 我有3个文件:

Index.php: index.php文件:

<html>
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="javascript.js"></script>
<head>
    <title>Test</title>
</head>
<body>
<div>
    <input type="button" value="Show" onclick="show('content','content.php');">
    <script type="javascript/text">
        show('content','content.php');
    </script>
    <br>
    <div id="content"></div>
</div>

content.php content.php

<div>CONTENT</div>
<input type="checkbox" id="checkbox" onclick="checkbox();">
<input type="text" id="textfield">

And javascript.js 和javascript.js

function show(divcontainer,file){
var xmlhttp;
if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();                     
    } 
    else {                                                  
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
                    document.getElementById(divcontainer).innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", file);                  
            xmlhttp.send();         
}
function checkbox(){
    if(document.getElementById('checkbox').checked){
        document.getElementById('textfield').value = "checked";
    }
    else{
        document.getElementById('textfield').value = "unchecked";
    }
}

When I press the button in index.php it loads the file content.php into the div container "content" via an ajax call. 当我按下index.php中的按钮时,它将通过ajax调用将content.php文件加载到div容器“ content”中。 The file content.php has a checkbox and a textfield. 文件content.php具有一个复选框和一个文本字段。 When the checkbox is checked and unchecked it executes a javascript function that puts checked/unchecked in the textfield. 选中和取消选中该复选框时,它会执行javascript函数,将选中/未选中的内容放入文本字​​段。 Simple enough and everything works fine. 很简单,一切正常。 Now what I want to do is to call the function checkbox() the moment that content.php is loaded. 现在,我要做的是在加载content.php的那一刻调用函数checkbox()。 In this example it would mean that the textbox would already have the value "unchecked" and not only after the checkbox has been checked and unchecked again. 在此示例中,这意味着文本框已经具有“未选中”的值,并且不仅在复选框已被选中并再次取消选中之后。

Putting the javascript code into the file content.php didn't do anything. 将javascript代码放入文件content.php中没有执行任何操作。

Somehow I feel that there must be a simple solution to this. 我以某种方式认为必须对此有一个简单的解决方案。 I'm still pretty new to javascript though so I don't know the in and outs yet. 我对javascript还是很陌生,所以我还不了解它的来龙去脉。

Any help would be greatly appreciated 任何帮助将不胜感激

Many thanks! 非常感谢!

Try this 尝试这个

   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        document.getElementById(divcontainer).innerHTML=xmlhttp.responseText;
        checkbox();
   }

if you´re new to JS you should definitely have a look at jQuery. 如果您不熟悉JS,那么您绝对应该看看jQuery。 Makes AJAX alot simpler. 使AJAX更加简单。

$.ajax({
    url: youURL, 
    success: function (){
        //or whatever function you want to be called after success :)
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM