2

我的表单 ID 是“requestquote”。当我提交表单时,我在控制台中收到一个错误('formSub 没有值')。表单过程正在通过我的 PHP 操作文件完成,但我也需要运行这段 javascript。任何建议,将不胜感激。

<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">
4

4 回答 4

3

如果它是表单的 ID,那么它应该是:

$('#requestquote');甚至$('form#requestquote');

$()不仅仅是GetElementByID。阅读 jQuery(真正的 Sizzle)选择器:http: //api.jquery.com/category/selectors/

于 2012-09-10T15:11:48.883 回答
0

这通常是由以下原因之一引起的:

  • 文档中根本没有具有给定 id 的元素
  • 在脚本运行时文档中没有具有给定 id 的元素(例如,当<script>出现在<form>源顺序中的之前时)

您可以通过重新排列源顺序或将 gEbId 调用包装在调用 onload / ondomready / 否则在表单元素存在后调用的函数中来处理时间问题。

于 2012-09-10T14:47:50.400 回答
0

从示例代码中很难看出。但是 JavaScript 可以在requestquote元素生成之前执行吗?

onLoad可以使用事件之类的东西来使 JavaScript 在所有 HTML 元素加载后执行。

于 2012-09-10T14:54:03.703 回答
0

在脚本的第三行,var formSub = $('requestquote');应该是var formSub = $('#requestquote');

# 向 jQuery 指示您要搜索 id 而不是标签名称。

于 2012-09-10T15:17:28.380 回答