简体   繁体   English

使用动态ajax更新相同的div

[英]update the same div with dynamic ajax

I have div1 with Jquery on click to update content dynamically with $ajax. 我在div1上单击了Jquery以使用$ ajax动态更新内容。

Later, the query change and I need to update the content again whenever the user click on div1 稍后,查询更改,每当用户单击div1时,我都需要再次更新内容

Both queries are working fine, however, the problem is on.click which updates the second query will load the first query result to the div not the 2nd updated query result. 两个查询都工作正常,但是问题就在。单击第二个查询的更新会将第一个查询结果加载到div而不是第二个更新的查询结果。

The example below explains something, not code correction though: 下面的示例说明了一些内容,但不是代码更正:

<div id='1'></div>

Query 1: $('1').click ($ajax('condition1+condition2')) -> output: string 1 查询1:$('1')。click($ ajax('condition1 + condition2'))->输出:字符串1

Result: 结果:

<div id='1'>string 1</div>

Query 2: $('1').click ($ajax('condition3+condition4')) -> output: string 2 查询2:$('1')。click($ ajax('condition3 + condition4'))->输出:字符串2

Result is still: 结果仍然是:

<div id='1'>string 1</div>

Is it becuase the same div id will fire the first happened condition? 是否因为相同的div ID将触发第一个发生的情况? How can I design this to update the on.click content dynamically with new conditions? 我该如何设计以在新条件下动态更新on.click内容?

I hope understand your question.....I'm guessing your first ajax call runs when the document is ready? 我希望能理解您的问题.....我猜您在文档准备好后就运行了您的第一个ajax调用? Then the second ajax call runs when the user clicks on the div? 然后,当用户单击div时,第二个ajax调用将运行。 but your problem is that your first ajax call is being called when the user clicks right? 但是您的问题是,当用户单击正确时,您的第一个ajax调用被调用了吗? If so this will solve it: 如果是这样,它将解决该问题:

I think the simple thing would be to create a variable "firstAjaxRan", set it to false. 我认为简单的事情是创建一个变量“ firstAjaxRan”,将其设置为false。 When the first ajax call runs check if "firstAjaxRan" is true, if not then run it then set it to true. 当第一个ajax调用运行时,请检查“ firstAjaxRan”是否为true,否则请运行,然后将其设置为true。 This will prevent the first ajax call from running again when the user clicks on the div. 这将防止用户单击div时再次运行第一个ajax调用。 Your 2nd ajax should run fine. 您的第二个ajax应该运行良好。

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

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