jQuery,如何使用多个ajax请求? [英] jquery, how to use multiple ajax requests?

查看:107
本文介绍了jQuery,如何使用多个ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在xxx.php内部有这种情况:

<form id="s">
<input id="q"/>
<input type="submit" href="Search!"/>
</form>
<div id="r"></div>

foo.php里面的

我有另一种形式:

inside the foo.php i have another form:

<form id="ss">
<input id="qq"/>
<input type="submit" href="Search!"/>
</form>
<div id="rr"></div>

yyy.php里面,我有另一种形式:

and inside the yyy.php i have another form:

<form id="sss">
<input id="qqq"/>
<input type="submit" href="Search!"/>
</form>
<div id="rrr"></div>

javascript.js内部,我有:

$('#s').submit(function(evt) {
evt.preventDefault();
$.ajax({
url: 'foo.php',
data: {
    query: $('#q').val()
  },
success: function(data) {
    $('#r').html(data);
  }
});
return false;
});

$('#ss').submit(function(evt) {
evt.preventDefault();
$.ajax({
url: 'yyy.php',
data: {
    query: $('#qq').val()
  },
success: function(data) {
    $('#rr').html(data);
  }
});
return false;
});

发生的事情是,当我提交第一个表单时,第二个加载了,所以现在我有2个表单(如预期的那样),但是当我提交第二个表单时,第三个表单没有使用ajax加载,但是它重定向到通过刷新.

what happens is that when i submit the first form the second one load in, so now i have 2 forms (as expected) but when i submit the second form the third one doesn't load using ajax, but it redirects to it by refreshing.

所以基本上我需要做尽可能多的ajax加载

so basically i need to do as many ajax loads as i can

有什么想法吗?

谢谢

推荐答案

对于动态加载的事情,您需要使用Live事件,以便您使用:

You need to use Live events for things you dynamically load in so in your case:

$("#ss").live('submit', function (e) {
   // Code here
});

您只需要对DOM中并不总是存在的事件使用实时事件.如果您通过ajax请求页面,然后打算使用它们,则需要使用实时事件.

You only need to use live events for things that are not always in the DOM. If you request pages through ajax and then intent to use them you will need to use live events.

请参阅: http://api.jquery.com/live/ 有关更多细节

See : http://api.jquery.com/live/ For a little more detail

这篇关于jQuery,如何使用多个ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆