3

阅读我列出了多个 IP 地址的列表框(dpAddress)。我选择了其中的几个,并希望使用以另一种形式 (loginForm) 提供的用户名-密码-域向每个选定的 IP 发送请求。如果我在列表中选择了 2 个 IP 地址,我已经验证循环可以工作 2 次。但它只打开一个新标签。

我想在表单提交后在浏览器的同一窗口中打开多个选项卡并显示结果。我怎样才能做到这一点?

function formSubmit1()
    {
     len = document.dpForm.dpAddress.length
     i = 0

     for (i = 0; i < len; i++) {
        if (document.dpForm.dpAddress[i].selected) {
            alert(document.dpForm.dpAddress[i].selected)
            var f = document.createElement("form");
            f.setAttribute('method',"post");


            var user = document.createElement("input"); //input element, text
            user.setAttribute('type',"text");
            user.setAttribute('name',"user");
            user.setAttribute('value',document.loginForm.user.value);

            var pass = document.createElement("input"); //input element, Submit button
            pass.setAttribute('type',"text");
            pass.setAttribute('name',"pass");
            pass.setAttribute('value',document.loginForm.pass.value);

            var domain = document.createElement("input"); //input element, Submit button
            domain.setAttribute('type',"text");
            domain.setAttribute('name',"domain");
            domain.setAttribute('value',document.loginForm.domain.value);

            f.appendChild(user);
            f.appendChild(pass);
            f.appendChild(domain);

            host = document.dpForm.dpAddress[i].value;

            address = "https://"+host+":9090/sys.login";
            f.setAttribute("target", "_blank");         
            f.setAttribute('action',address);
            f.submit();
        } 
     } 
    }
4

1 回答 1

0

使其工作的一种方法是给窗口一个唯一的目标属性并在提交表单之前打开它们。

function formSubmit1() {
    len = document.dpForm.dpAddress.length
    i = 0

    for (i = 0; i < len; i++) {
        if (document.dpForm.dpAddress[i].selected) {

            // ... other code goes here

            address = "https://" + host + ":9090/sys.login";
            f.setAttribute("target", "window-" + i); // a unique id instead of "_blank"
            f.setAttribute('action', address);
            window.open(address, "window-" + i); // pop open a window with that same id and the form will submit into it
            f.submit();
        }
    }
}

这是一个功能演示,展示了该技术的实际应用(确保您的弹出窗口阻止程序已禁用)。

于 2013-04-23T02:52:32.773 回答