繁体   English   中英

如何运行多个函数然后回调

[英]how to run multiple functions and then a callback

我又一次“涉足”了 javascript 和 jQuery 的黑暗艺术,而对我实际上在做什么却一无所知。 我所知道的是,我设法通过抄袭其他代码的一些位并对其进行一些修改来将我的代码转换为 function,并且经过大量的诅咒之后,它以一种时尚的方式工作了。

下面是我的一段代码,它正确构建并显示了一个动态选择框,其中包含从 mysql 查询返回的值。 当用户从列表中选择一个项目时,代码成功运行 function,它显示与该项目关联的 map。

What I ACTUALLY need it to do is also run the code to display the map for the default/top item returned by the query first, and THEN whenever a new item is selected it should run the function to display the newly selected map.

我希望解释已经足够清楚,但如果没有,请随时责备我。

非常感谢,

抢。

 $(document).ready(function() {
$('#selecthere').load('/temp/php_script.php?r='+ random, function () 
 { //callback
$('select', this).change(function () { //catch onchange event of select
//call your function here, pass the selected value
initialize($(this).val());
});
});
});

(根据以下评论进行编辑。)

如果我理解的话, initialize(val)是绘制与val关联的 map 的 function ,并且您希望在页面加载时运行它val是默认情况下 Z99938282F04061859941E18F 中的任何内容。 为此,您只需在ready function 中添加 function 调用:

$(document).ready(function(){
    var random = Math.random();
    // load contents of php script into #selecthere element
    $('#selecthere').load('/temp/php_script.php?r='+ random, function (){ 
       // the contents of the php script are now loaded
       // go ahead and call initialize() on the value that is selected by default
       initialize($('select[name=title]').val());
       // attach a function to fire when select element is changed
       $('select[name=title]').change(function () { 
           // here 'this' refers to the select element
           // call initialize() with the value of select element
           initialize($(this).val());
       });
    });
});

希望我明白了。

查看JQuery 事件 API 这样,当发生不同的用户交互时,您可以运行函数。 小例子:

$('#foo').bind('click', function() {
  alert('User clicked on "foo."');
});

这将在单击 ID 为 #foo 的项目时运行。

暂无
暂无

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

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