简体   繁体   English

与Ajax通话仅工作两次

[英]Ajax call with works only twice

I have a strange one. 我有一个奇怪的人。

I have a div which has an onclick event, calling a function, which in turn makes an ajax call. 我有一个具有onclick事件的div,它调用一个函数,而该函数又进行了ajax调用。 Doing so in Safari works exactly twice. 这样做在Safari中可以正常工作两次。 Running the same program with an iPhone works perfectly every time. 每次使用iPhone运行相同的程序都能完美运行。

This is is what I am doing: 这就是我在做什么:

// this is created by the Ajax call
no1 = 0;
no2 = 1;
formName = "myButton" + no1 + no2;
//newItem is what is returned by the Ajax call
newItem = "<div id=\"trainingOptions\" onclick=\"request_ajax('".$formName."')\">";
//this places the onclick event into the HTML code
document.getElementById.("myDiv").innerHTML = newItem;

So basically this is how the onclick event is placed over and over again. 因此,基本上,这就是onclick事件一遍又一遍地放置的方式。 This part is replaced with exactly the same, when request_ajax answers. 当request_ajax答复时,这部分将替换为完全相同的部分。

Now this works exactly two times in Safari and as often as clicked on the iPhone. 现在,该功能在Safari中运行两次,与在iPhone上单击的频率相同。

Any ideas why this happens? 任何想法为什么会发生这种情况?

Best Kurt 最佳库尔特

You are giving very few elements for us to help you. 您提供的要素很少,无法帮助我们。 So this is just a guess which is the following: 因此,这只是以下猜测:

Your appending: 您的附加内容:

newItem = "<div id=\"trainingOptions\" onclick=\"request_ajax('".$formName."')\">";

As you are not clearing the div before you append it, it seems to me that you are appending multiple divs with the same id. 由于您没有在添加div之前清除div,在我看来,您正在添加具有相同id的多个div。 As browsers handle multiple equivalent IDs in different ways, this is what might be causing you your problem. 由于浏览器以不同方式处理多个等效ID,因此这可能是导致您遇到问题的原因。

There should only be one element with one ID, you can't have two elements with the same ID this can cause conflicts. 只能有一个具有一个ID的元素,而不能有两个具有相同ID的元素,这会导致冲突。 So maybe that's what is happening to you. 所以也许这就是您正在发生的事情。

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

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