简体   繁体   English

准备就绪的AJAX级联DropDown JavaScript事件

[英]AJAX Cascading DropDown JavaScript event onready

I'd like to fire a JavaScript function when AJAX returns the values of drop down items. 我想在AJAX返回下拉项的值时触发JavaScript函数。

The scenario is: 该方案是:

function 1 fires -> ajax gets items from database -> the dropdown items are filles -> my javascript function is called. 函数1触发-> ajax从数据库中获取项目->下拉项目为填充->我的javascript函数被调用。

Does any of you have idea how to make a handler for such event ? 你们中有谁知道如何为此类事件创建处理程序?

As you've tagged the question with jQuery, I'll show you a jQuery solution: 当您使用jQuery标记问题后,我将向您展示jQuery解决方案:

$.post("someScript.php", function(data) {
    /*This callback function is executed when the AJAX call returns successfully
    You would do something with your dropdown items here
    and then call your next function.*/
});

The various jQuery AJAX methods ( post , get , load for example) all provide a way to pass a callback as an argument. 各种jQuery AJAX方法(例如postgetload )都提供了一种将回调作为参数传递的方法。 The callback is executed upon a successful response from the server. 在服务器成功响应后执行回调。 Inside that callback function you can execute whatever code you need to, to deal with data which contains the response. 在该回调函数中,您可以执行所需的任何代码来处理包含响应的data

If you're not using jQuery, I'll assume you already have an XMLHttpRequest object. 如果您不使用jQuery,则假定您已经有一个XMLHttpRequest对象。 XMLHttpRequest has a property called onreadystatechange to which you can assign a function that runs when the state changes: XMLHttpRequest具有一个称为onreadystatechange的属性,您可以向其分配一个在状态更改时运行的函数:

xhr.onreadystatechange = function(){
    if(xhr.readyState == 4) {
        //This will be reached if the call returns successfully
    }
}

The idea is the same as the jQuery method shown above. 这个想法与上面显示的jQuery方法相同。

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

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