2010-06-08 82 views
1

我改變形式行動:錯誤的變化形式行動

window.onload = function() { 
    document.getElementById('pdf').onclick = addExportEvent; 
    document.getElementById('xls').onclick = addExportEvent; 
    document.getElementById('xml').onclick = addExportEvent; 
    document.getElementById('csv').onclick = addExportEvent; 
} 

function addExportEvent() { 
    data = grid.getAllGridData(); 
    document.getElementById('dados').setAttribute('value', encodeURIComponent(JSON.stringify(data))); 

    formulario = document.getElementById('formulario'); // Line 55! 
    formulario.action = 'php/' + this.id + '.php'; 
    formulario.submit(); 

    return false; 
} 

不過,這並不與Internet Explorer工作。它返回以下錯誤:

Message: The object doesn't support the property or method. 
Line: 55 
Character: 2 
Code: 0 
URI: http://www.site.com/javascript/scripts.js 
+0

不要忘記在Javascript聲明變量時使用'var'關鍵字。例如'var data = grid.getAllGridData()'。這將在以後節省您的相關頭痛問題。 – 2010-06-08 14:01:52

+1

請注意,Internet Explorer 7及更早版本的腳本錯誤行號非常不準確。這在IE 8中得到了改進,但對於我所知的所有情況,可能仍然存在行號不正確的情況。 – 2010-06-08 14:03:41

回答

1

我認爲安迪E的頭評論擊中了它,好,頭。你正在分配正確的元素,但沒有聲明它使用變種,這使IE插件和窒息和各種不好的東西。其他瀏覽器處理這一點很好。所以,你想,而不是訪問它聲明,這意味着它永遠不會id的值的formulario:formulario

function addExportEvent() { 
    var data = grid.getAllGridData(); 
    document.getElementById('dados').setAttribute('value', encodeURIComponent(JSON.stringify(data))); 

    var formulario = document.getElementById('formulario'); // Line 55! 
    formulario.action = 'php/' + this.id + '.php'; 
    formulario.submit(); 

    return false; 
} 
+0

但是formulario是一個HTML元素,它可以在Firefox,Google Chrome,Opera等瀏覽器中運行... 在我之前: formulario.setAttribute('action','php /'+ this.id +'.php 「); – Thomas 2010-06-08 14:02:00

+0

安迪是對的^看到修改^ – 2010-06-08 14:05:28

+0

Yeap,安迪是對的!我把var關鍵字和一切工作! 謝謝你們! – Thomas 2010-06-08 14:07:15