2012-04-17 35 views
2

使用按鈕onclick事件設置window.location時,結果在第二個參數名稱後被截斷。帶按鈕onclick的window.location截斷URL

此代碼:

<button onclick="window.location='index.php?p=reports_manage&id=new'">create new report - button</button> 

正在發送的用戶此頁:

https://foo.com/index.php?p=reports_manage&id= 

即使我在最後它還是被在同一個地方切斷添加額外的參數。

所有這些工作,但罰款:

<a href="index.php?p=reports_manage&id=new">create new report - link</a> 
<a href="javascript:window.location='index.php?p=reports_manage&id=new'">create new report - JS1</a> 
<a href="javascript:void(0);" onclick="window.location='index.php?p=reports_manage&id=new'">create new report - JS2</a> 

任何想法可能是什麼造成的?據我可以告訴按鈕工作得很好,打破了沒有任何改變這個代碼。還有什麼可能影響到這一點?頁面上沒有javascript錯誤,無論使用什麼瀏覽器,都會發生此錯誤。

+0

[適用於我](http://jsfiddle.net/gthtB/) – qwertymk 2012-04-17 22:02:03

+0

DOM檢查器是否顯示與您的HTML代碼相同或可能存在解析問題? – Bergi 2012-04-17 22:02:40

+1

您是否嘗試過使用而不是按鈕元素?我建議總是使用按鈕輸入作爲瀏覽器有不同的,有時意外的行爲

回答

3

使用<input type="button" />而不是<button></button>,因爲它有意外的行爲(如果使用提交表單它甚至會提交內容!)

+0

謝謝。我最初使用

+2

在@ trickyzter的回答中,使用'

4

一個長鏡頭,但也許指定按鈕類型會有所幫助。在這種情況下,鍵入=「按鈕」。

@William:不同的瀏覽器有不同的先決條件,但作爲一種保護措施,它總是很好的被明確。 ;)