错误检查在Javascript中不起作用
Error checking is not working in Javascript
我目前正在学习Javascript,并且对以下脚本有一些问题。似乎我还没有声明函数。任何帮助都将不胜感激。谢谢
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 7: Example 4</title>
<script type="text/javascript">
function btnCheckForm_onclick() {
var myForm = document.form1;
if (myForm.txtAge.value == "" || myForm.txtName.value == "") {
alert("please complete the form");
if (myForm.txtName.value == "") {
myForm.txtName.focus();
} else {
myForm.txtAge.focus():
} else {
alert("Thanks for completing the form");
}
}
function txtAge_onblur() {
var txtAge = document.form1.txtAge;
if (isNaN(txtAge.value) == true) {
alert("please enter a valid age");
txtAge.focus();
txtAge.select();
}
}
function txtName_onchange() {
window.status = "Hi " + document.form1.txtName.value;
}
</script>
<body>
<form action="" name="form1">
Please enter the following details:
<p>
Name:
<br />
<input type="text" name="txtName" onchange="txtName_onchange()" />
</p>
<p>
Age:
<br />
<input type="text" name="txtAge" onblur="txtAge_onblur()" size="3" maxlength="3" />
</p>
<p>
<input type="button" value="Check Details" name="btCheckForm" onclick="btnCheckForm_onclick()">
</form>
</body>
</html>
您有一些语法错误
function btnCheckForm_onclick() {
var myForm = document.form1;
if (myForm.txtAge.value == "" || myForm.txtName.value == "") {
alert("please complete the form");
if (myForm.txtName.value == "") {
myForm.txtName.focus();
} else {
myForm.txtAge.focus();
} }
else {
alert("Thanks for completing the form");
}
}
function txtAge_onblur() {
var txtAge = document.form1.txtAge;
if (isNaN(txtAge.value) == true) {
alert("please enter a valid age");
txtAge.focus();
txtAge.select();
}
}
function txtName_onchange() {
window.status = "Hi " + document.form1.txtName.value;
}
<form action="" name="form1">
Please enter the following details:
<p>
Name:
<br />
<input type="text" name="txtName" onchange="txtName_onchange()" />
</p>
<p>
Age:
<br />
<input type="text" name="txtAge" onblur="txtAge_onblur()" size="3" maxlength="3" />
</p>
<p>
<input type="button" value="Check Details" name="btCheckForm" onclick="btnCheckForm_onclick()">
</form>
相关文章:
- 为什么这个 .push 不起作用?(JavaScript)
- document.createelement不起作用(javascript)
- Chrome 中表格的 CSV 导出不起作用 - JavaScript/AngularJS
- 如果语句不起作用 - JavaScript
- 为什么我的动态更改链接不起作用?(Javascript)
- RPS 函数不起作用(JavaScript)
- 计时器间隔不起作用 Javascript
- 通过返回 false - 不起作用(javascript)来阻止链接打开
- Eval 不起作用 - JavaScript
- Android 浏览器 .click() 不起作用 javascript
- 为什么这段代码不起作用?Javascript
- jsFiddle显示正确,但一旦在本地运行就不起作用(Javascript)
- 带有html的Navigator不起作用javascript
- 从文本文件中提取内容不起作用 JavaScript
- 从查询字符串中获取参数;不起作用..Javascript
- regex手机验证不起作用-javascript
- if和else语句不起作用(javascript)(html)
- 更改字体大小的复选框不起作用-Javascript
- 为什么这个基本的 if 语句不起作用?JavaScript
- Timer 不起作用/ JavaScript/ innerHTML null