2015-07-21 173 views
0

我已經寫了一個腳本,它或多或少都會打開相同的鏈接,但會與另一個ID打開。循環中的變量是哪個。我的劇本其實是這樣的,我只想知道我可以打開與變量此鏈接爲ID:在循環中打開鏈接 - JavaScript

<!DOCTYPE html> 
<html> 
<body> 

<button onclick="openLinks();">Click </button> 

<script> 
    function openLinks() { 
     var i; 

     for (i = 150; i < 156; i++) { 
      window.open('http://www.ortner.elmima.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id="i"'); //doesn't work, should print 150, 151... 
     } 
    } 
</script> 

</body> 
</html> 
+0

在控制檯試試這個:' 「STORE_ID =」 + 3' –

+3

彈出式窗口攔截將阻止.... – epascarello

+0

這是行不通的。不,彈出式窗口攔截器沒有問題,我使用它作爲個人用途,我停用了這個html網站的攔截器 –

回答

0

如果你把雙引號單引號裏面的JS引擎將它理解爲一個常規字符不是變量。 「和」是字符串常量,但是,你應該只使用其中的一個

你應該寫這樣的事情

var link = "someurl" + i; 
0

你有string/number級聯問題:

變化:

window.open('http://www.ortner.elmima.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id="i"'); 

window.open('http://www.ortner.elmima.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id='+i); 

store_id="i"'中的i不會被動態評估。您必須將i作爲字符串「取出」到連接字符串的動態循環變量。

0

你將如何建立任何正常的字符串?

var x = "asdfg" + i; 

所以你在做什麼也沒有什麼不同。

....tor&action=edit_store&store_id=' + i) 
0

應該

window.open('http://www.ortner.elmima.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id="' + i + '"'); 

使i不會呈現爲一個字符串的一部分,而是指的是可變的。

0

它看起來像你沒有將變量作爲字符串添加到你的url。

嘗試是這樣的:

window.open("http://www.ortner.elmima.at/wp-admin/admin.php?page=wpsl_store_editor&action=edit_store&store_id=" + String(i)); 
+0

謝謝你,工作。 –

+1

這根本沒有必要。 –

+0

沒問題。 :-) @SebastianNette如何? – aznbanana9