给定一个以逗号分隔的 URL 字符串,我想为其转换为数组的每个元素打开一个单独的窗口,然后提交一个表单。
以下代码仅打开一个窗口,并且不进行表单提交:
$.each(urlList.split(","), function (index, item) {
urlList = "http://www.cnn.com,http://www.foxnews.com";
_gaq.push(['_trackEvent', 'Results', 'Click', 'Classifieds+: ' + item + ' : ' + SourceUrlProviderID]);
window.open(item, "_blank");
});
document.forms[0].submit();
当我执行以下操作时,我得到了所需的操作 - 打开三个新选项卡/窗口,但我看起来表单提交将完成两次并且不太有意义:
urlList = "http://www.cnn.com,http://www.foxnews.com";
// opens windows for each URL in the string
$.each(urlList.split(","), function (index, item) {
_gaq.push(['_trackEvent', 'Results', 'Click', 'Classifieds+: ' + item + ' : ' + SourceUrlProviderID]);
document.forms[0].submit();
window.open(item, "_blank");
});
有人可以解释一下吗?
最佳答案
为什么urlList
在$.each
里面?无论如何,我尝试过这个,并且有效:
var urlList = "http://www.cnn.com,http://www.foxnews.com"
$.each( urlList.split( "," ), function( index, item ) {
window.open( item, "_blank" )
});
document.forms[0].submit()
关于javascript - 如何使用 jQuery/js 打开多个窗口并触发表单提交事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14651204/