Javascript GetElementByID没有值
我的表单ID是“requestquote”。当我提交表单时,我在控制台中收到一个错误('formSub没有值')。表单过程是通过我的PHP操作文件完成的,但我也需要这一点javascript来运行。如有任何建议,将不胜感激Javascript GetElementByID没有值,javascript,jquery,Javascript,Jquery,我的表单ID是“requestquote”。当我提交表单时,我在控制台中收到一个错误('formSub没有值')。表单过程是通过我的PHP操作文件完成的,但我也需要这一点javascript来运行。如有任何建议,将不胜感激 <script> _opt.push(['instrument','requestquote']); var formSub = $('requestquote'); console.log(formSub); formSub.submit(function
<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>
_选择推送(['instrument','requestquote']);
var formSub=$('requestquote');
控制台日志(formSub);
formSub.submit(函数(){
_opt.modifyFormParameters=函数(参数){
参数[“_orig_action”]=document.getElementById(“_orig_action”).value;
参数[“\u opt\u vid”]=document.getElementById(“\u opt\u vid”).value;
参数[“\u opt\u visit”]=document.getElementById(“\u opt\u visit”).value;
参数[“\u opt\u cid”]=document.getElementById(“\u opt\u cid”).value;
参数[“\u opt\u url”]=document.getElementById(“\u opt\u url”).value;
参数[“_opt_paget”]=document.getElementById(“_opt_paget”).value;
参数[“first_name”]=document.getElementById(“name”).value;
参数[“公司”]=document.getElementById(“公司”).value;
参数[“网站”]=document.getElementById(“url”).value;
参数[“email”]=document.getElementById(“email”).value;
参数[“phone”]=document.getElementById(“phone”).value;
返回参数;
};
_opt.push(['submit','requestquote']);
_opt.the_form=此;
setTimeout(函数(){u opt.the_form.submit();},1000);
返回false;
});
这是表格代码
<form id="requestquote" action="/quote-process/contact_pr_EPH.php" method="post">
这通常是由以下原因之一引起的:
- 文档中根本没有具有给定id的元素
- 运行脚本时文档中没有具有给定id的元素(例如,当源代码顺序中的
出现在
之前时)
您可以通过重新排列源代码顺序,或者将gEbId调用包装到一个函数中来处理计时问题,该函数在表单元素存在后被onload/ondomready/调用。很难从示例代码中分辨出来。但是JavaScript是否可以在创建
requestquote
元素之前执行
类似于
onLoad
事件的事件可用于使JavaScript在所有HTML元素加载后执行。如果它是表单的ID,那么它应该是:
$('requestquote')
甚至$('form#requestquote')代码>
$()
不仅仅是GetElementByID
。
阅读jQuery(真正的Sizzle)选择器:在脚本的第三行,var formSub=$('requestquote')代码>应该是var formSub=$('#requestquote')代码>
#向jQuery指示您要搜索id而不是标记名。请确保在HTML中定义元素后运行此操作。除此之外,没有足够的信息。您可以添加
的代码吗?脚本是页脚正文中的最后一个内容。phpI认为您需要提供一个完整的最小测试用例来充分说明问题。等等,您在提交表单时遇到错误。这表明问题在onsubmit处理程序中的某个地方。我想如果你想让我们提供帮助,你需要向我们展示更多的代码。他已经告诉我们“脚本是footer.php中正文中的最后一个内容”…我添加了一个页面加载jQuery函数。。“formSub”仍接收空值。非常明显,但很难检测;)