[英]why does javascript skip some of my functions?
当我运行该程序时,validatePhone(); validateAddress(); 和validateCity(); 被完全跳过了,为什么? 这是我的JS:
function validatePage()
{
var valid = false;//sets valid.
var msg = "";//sets message to blank.
validateFname();
function validateFname()
{
var fnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validateLname();
}
function validateLname()
{
var lnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
if(lastName.value.match(lnameTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validatePhone();
}
function validatePhone()
{
var nameTxt = /^[0-9]+$///sets valid inputs for recipient name.
if(Phone.value.match(nameTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validateAddress();
}
function validateAddress()
{
var addressTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
if(address1.value.match(addressTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validateCity();
}
function validateCity()
{
var cityTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
if(cityTown.value.match(cityTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validatePostcode();
}
function validatePostcode()
{
var postcodeTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
if(postcode.value.match(postcodeTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
}
if(valid == true)
{
window.open("checkout_step_5.html");
}
else
{
msg="Not all required fields were filled."
alert(msg);
return false;
}
}
我检查了拼写错误,没有发现我注意到我真的不知道为什么这不起作用吗?
截至目前,我尝试将整个代码放入try catch中,但未发现任何异常
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function validatePage()
{
try{
var valid = false;//sets valid.
var msg = "";//sets message to blank.
validateFname();
function validateFname()
{
var fnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validateLname();
}
function validateLname()
{
var lnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
if(lastName.value.match(lnameTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validatePhone();
}
function validatePhone()
{
var nameTxt = /^[0-9]+$///sets valid inputs for recipient name.
if(Phone.value.match(nameTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validateAddress();
}
function validateAddress()
{
var addressTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
if(address1.value.match(addressTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validateCity();
}
function validateCity()
{
var cityTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
if(cityTown.value.match(cityTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
validatePostcode();
}
function validatePostcode()
{
var postcodeTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
if(postcode.value.match(postcodeTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
}
}
if(valid == true)
{
window.open("checkout_step_5.html");
}
else
{
msg="Not all required fields were filled."
alert(msg);
return false;
}
}catch(e){
alert(e);
}
}
</script>
</head>
<body onload="validatePage()">
</body>
</html>
另一个建议是,一旦Valid变为false,请使用return突破功能。 总是检查一个参数是否为false毫无意义。
if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
{
valid = true;
}
else
{
valid = false;
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.