简体   繁体   English

打开PHP而不中断JavaScript循环

[英]Open PHP without breaking JavaScript loop

I'm iterating over a table's values to modify them if any changes have been made by the user. 如果用户进行了任何更改,我将遍历表的值以修改它们。

Currently I have something similar to: 目前我有类似的东西:

for (var i = 0; i < rows.length - 1; i++) {
    if (item[5]) {
        window.open("modify.php?id=" + id + "&delete=true");
    }
}

My question is how I can connect to my modify.php file without breaking the for loop. 我的问题是如何在不中断for循环的情况下连接到我的Modify.php文件。 I tried with 我尝试过

window.location("modify.php?id=" + id + "&delete=true");

too but that's the same story. 也是,但这是同一回事。

I considered using window.open and setting the new windows dimension's to something very small so it appears hidden and closing it again after my PHP has finished executing. 我考虑过使用window.open并将新的Windows尺寸设置为非常小的尺寸,因此它看起来是隐藏的,并在我的PHP完成执行后再次将其关闭。

This however seems ridiculously dumb hence why I'm wondering what a/the better approach would be? 但是,这似乎荒唐可笑,因此为什么我想知道哪种/更好的方法是什么?

As with any other code, why not simply copy the instructions for later use? 与其他任何代码一样,为什么不简单地复制说明以备后用?

var openItems = [], i;

for (i = 0; i < rows.length - 1; i++) {
    if (item[5]) {
        openItems.push(id); // Don't open the window just yet, just copy to array
    }
}

for (i = 0; i < openItems.length; i++) {
    window.open("modify.php?id=" + openItems[i] + "&delete=true");
}

And if you consider that you'd only be opening one window anyway (and you want the first item to be it), then simply do the following: 而且,如果您认为您无论如何都只会打开一个窗口(并且您想让它成为第一个项目),那么只需执行以下操作:

var openID, i;

for (i = 0; i < rows.length - 1; i++) {
    if (item[5]) {
        openID = i;
        break; // No need to continue iterations
    }
}

if (openID) {
    window.open("modify.php?id=" + openID + "&delete=true");
}

If you want the last item to be the one you open, simply remove the break; 如果您希望最后一个项目是您打开的项目,只需删除break; statement. 声明。

It sounds like you want to be using AJAX here. 听起来您想在这里使用AJAX。 For conciseness, I'll show you what I mean using jQuery.ajax : 为简洁起见,我将向您展示使用jQuery.ajax含义:

for (var i = 0; i < rows.length - 1; i++) {
    if (item[5]) {
        $.ajax("modify.php?id=" + id + "&delete=true");
    }
}

This sends an asynchronous HTTP GET request to the URL provided. 这会将异步HTTP GET请求发送到提供的URL。

AJAX can be done in vanilla JavaScript too but for reasons of convenience and compatibility I would recommend using a library. AJAX也可以在原始JavaScript中完成,但出于方便和兼容性的考虑,我建议使用库。

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

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