gpt4 book ai didi

Javascripting 没有按预期工作,并且没有返回语法错误?

转载 作者:行者123 更新时间:2023-11-29 14:39:35 26 4
gpt4 key购买 nike

好的,我正在尝试使用 JavaScript 来验证表单,然后在使用正确的信息验证所有表单后显示“谢谢”消息。我可以使用它来验证名字、电子邮件地址和 radio 选择,但是我似乎无法使用它来验证状态选择或显示感谢消息,但我也使用了我的浏览器(Firefox) 用于挖掘语法错误的开发控制台。没有显示。有帮助吗?

一小段代码在下面,一个指向整个代码的 jsfiddle 链接,下面是 HTML。

function validForm() { // reference point 
/* first name, email, comments and phone */
var fname = document.form1.fname;
var email = document.form1.email;
var comments = document.form1.comments;
var phone1 = document.form1.phone1;
var phone2 = document.form1.phone2;
var phone3 = document.form1.phone3;
/* collecting the error spans for later use */
var fnErr = document.getElementById('fnErr');
var lnErr = document.getElementById('lnErr');
var emErr = document.getElementById('emErr');
var phErr = document.getElementById('phErr');
var cmErr = document.getElementById('cmErr');
var state = document.getElementById('stErr');
if(fname.value=="") {
fnErr.innerHTML = "We need your name, please.";
fname.focus();
return false;
} else {
fnErr.innerHTML = "";
}
if(email.value=="") {
emErr.innerHTML = "We need your email, please.";
email.focus();
return false;
} else {
emErr.innerHTML = "";
}
if(state.value=="none") {
fnErr.innerHTML = "Please select your state.";
state.focus();
return false;
} else {
fnErr.innerHTML = "";
}
/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i;
if(isNaN(phone1.value) || isNaN(phone2.value) ||
isNaN(phone3.value)) {
phErr.innerHTML = "If you include a phone number it should be numbers only.";
phone1.select();
return false;
} else {
phErr.innerHTML = "";
}
var notices = document.form1.notices;
var selected;
for(var i=0;i<notices.length;i++) {
if(notices[i].checked) {
selected = notices[i].value;
}
}
/* set up a message for a advert recipient */
var msg = "Thank you, <b>"+fname.value
+"!</b> Your message has been received and one of our representatives will reply to <a href='"
+email.value+"'>"+email.value+"</a> within a day or two. Relax, and enjoy the rest of the site!";
if(selected != undefined) {
document.getElementById('notePref').innerHTML = "";
if (selected == "yes") { // here's where we test the contents of "selected"
document.getElementById('fnlMsg').innerHTML = msg;
} else {
document.getElementById('fnlMsg').innerHTML = "<strong>"+fname.value+"</strong>, thank you for your feedback!";
}
var contactForm = document.getElementById('registerForm').setAttribute('class','hide');
var loseSep = document.getElementsByClassName('spec');
loseSep[0].setAttribute('class', 'hide');
} else {
document.getElementById('notePref').innerHTML = "Please make a selection:";
}
} // reference point

https://jsfiddle.net/xp5e099y/2/

最佳答案

首先:您没有 id 为 ="fnlMsg"的位置(div/span/等)

第二:你没有 id 为 ="contactForm"的地方(div/span/etc)

然后改变你的html

<select name="state">

<select id="state">

和你的脚本

var state = document.getElementById('stErr');

var state = document.getElementById('state');

关于Javascripting 没有按预期工作,并且没有返回语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40519026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com