我无法理解为什么我的javascript无法运行...我是否需要在某处声明变量?
<script type="text/javascript">
function validation(form) {
if(form.first_name.value == '' ) {
alert('Please enter your first name');
form.first_name.focus();
return false;
}
if(form.00N30000006S4uq.value == '') {
alert('Please enter the high end of your budget');
form.company.focus();
return false;
}
return true;
}
</script>
<form action="https://www.salesforce.com/servlet/servlet.WebToLead" method="POST" onsubmit="return validation(this);">
答案 0 :(得分:1)
表格名称需要以字母开头。 “00N30000006S4uq”失败,因为它以数字开头。
答案 1 :(得分:1)
如@ReturnTrue所述, NAME 必须以字母开头。这就是你的脚本失败的原因。
在你的情况下,因为字段是自动生成的,如果你知道表单中元素的流程,那么你可以引用表单元素数组,就像这样......
form.elements[2].value
其中form.elements[2]
为form.00N30000006S4uq
。那将完成这项工作。
示例:强>
function validation(form) {
if(form.elements[0].value == '' ) {
alert('Please enter your first name');
form.first_name.focus();
return false;
}
if(form.elements[2].value == '') {
alert('Please enter the high end of your budget');
form.company.focus();
return false;
}
return true;
}
<form action="" method="POST" onSubmit="return validation(this);">
<input type="text" name="first_name" />
<input type="text" name="company" />
<input type="text" name="00N30000006S4uq" />
<input type="submit" name="submit" />
</form>