2012-01-16 32 views
0

我想在AJAX返回下拉项目的值时触发JavaScript函数。AJAX级联DropDown JavaScript事件onready

的情况是:

功能1起火灾 - > AJAX会从数据库项目 - >下拉项FILLES - >我的JavaScript函数被调用。

你们有没有想法如何为这样的事件做一个处理程序?

回答

1

正如您所标记的使用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.*/ 
}); 

各种jQuery的AJAX方法(postgetload例如)都提供一种方式来传递作为参数的回调。回调在服务器成功响应时执行。在回调函数内部,您可以执行任何您需要的代码,以处理包含响应的data

如果你不使用jQuery,我假设你已经有一个XMLHttpRequest对象。 XMLHttpRequest有一个称为onreadystatechange到可以给它分配在运行状态变化时的功能属性:

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

的想法是相同的,如上所示jQuery的方法。

+0

太棒了! jQuery做了诡计。非常感谢! – 2012-01-16 13:25:48

+0

没问题,很高兴我可以帮忙:) – 2012-01-16 13:29:06