2012-09-10 122 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,而比标签名称。

相关问题