2012-09-10 44 views
2

我的表單ID是「requestquote」。當我提交表單時,我在控制檯收到一個錯誤('formSub沒​​有任何價值')。表單過程正在通過我的PHP動作文件完成,但我需要這一點的JavaScript來運行。任何建議,將不勝感激。Javascript GetElementByID沒有值

<script> 
_opt.push(['instrument','requestquote']); 

var formSub = $('requestquote'); 
console.log(formSub); 

formSub.submit(function(){ 

_opt.modifyFormParameters = function(parameters){ 

parameters["_orig_action"] = document.getElementById("_orig_action").value; 
parameters["_opt_vid"] = document.getElementById("_opt_vid").value; 
parameters["_opt_visit"] = document.getElementById("_opt_visit").value; 
parameters["_opt_cid"] = document.getElementById("_opt_cid").value; 
parameters["_opt_url"] = document.getElementById("_opt_url").value; 
parameters["_opt_paget"] = document.getElementById("_opt_paget").value; 
parameters["first_name"] = document.getElementById("name").value; 
parameters["company"] = document.getElementById("company").value; 
parameters["website"] = document.getElementById("url").value; 
parameters["email"] = document.getElementById("email").value; 
parameters["phone"] = document.getElementById("phone").value; 

return parameters; 
}; 
_opt.push(['submit', 'requestquote']); 
_opt.the_form = this; 

setTimeout(function(){ _opt.the_form.submit(); } , 1000); 
return false; 
}); 
</script> 

這裏是表單代碼

<form id="requestquote" action="/quote-process/contact_pr_EPH.php" method="post"> 
+0

請確保在HTML中定義元素後運行此操作。除此之外,沒有足夠的信息。 – pimvdb

+1

請問您可以添加'

'的代碼嗎? – Spontifixus

+0

該腳本是footer.php – Garrett

回答

0

這通常是通過以下之一引起的:

  • 不具有在所有
  • 不與所述文檔中的給定的ID的元素在腳本運行時在文檔中有一個具有給定id的元素(例如,<script>出現在<form>之前) E產品的訂購)

您可以通過重新排列源順序,或通過在被調用函數包裹gEbId呼叫處理時間問題的onload/ondomready /表單元素存在,否則後。

+0

我已經添加了一個頁面加載jQuery函數..仍然接收null爲'formSub'。 – Garrett

0

從示例代碼很難說。但是,在執行requestquote元素之前JavaScript能否執行?

可以使用類似onLoad事件的方式在所有HTML元素加載後執行JavaScript。

+2

他已經告訴「腳本是在footer.php身體內的最後一件事......」 – Spontifixus

0

在腳本中的第三行,var formSub = $('requestquote');應該var formSub = $('#requestquote');

的#指示jQuery的,你要搜索一個id,而比標籤名稱。

相關問題