Ajax 调用只能运行两次

发布于 2024-12-10 11:52:37 字数 657 浏览 0 评论 0原文

我有一个奇怪的。

我有一个 div,它有一个 onclick 事件,调用一个函数,该函数又进行 ajax 调用。 在 Safari 中执行此操作正好两次。 使用 iPhone 运行相同的程序每次都能完美运行。

这就是我正在做的事情:

// 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;

所以基本上这就是一次又一次放置 onclick 事件的方式。 当request_ajax应答时,这部分被替换为完全相同的。

现在,该操作在 Safari 中的运行次数恰好是 iPhone 上点击次数的两倍。

有什么想法为什么会发生这种情况吗?

最佳库尔特

I have a strange one.

I have a div which has an onclick event, calling a function, which in turn makes an ajax call.
Doing so in Safari works exactly twice.
Running the same program with an iPhone works perfectly every time.

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.
This part is replaced with exactly the same, when request_ajax answers.

Now this works exactly two times in Safari and as often as clicked on the iPhone.

Any ideas why this happens?

Best Kurt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-12-17 11:52:37

您提供的信息很少,我们无法帮助您。所以这只是一个猜测,如下所示:

你的附加:

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

由于你在附加它之前没有清除 div,所以在我看来,你正在附加具有相同 id 的多个 div。由于浏览器以不同的方式处理多个等效 ID,这可能是导致您出现问题的原因。

只能有一个具有一个 ID 的元素,不能有两个具有相同 ID 的元素,否则会导致冲突。所以也许这就是发生在你身上的事情。

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. As browsers handle multiple equivalent IDs in different ways, this is what might be causing you your problem.

There should only be one element with one ID, you can't have two elements with the same ID this can cause conflicts. So maybe that's what is happening to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文