gpt4 book ai didi

javascript - 如果函数中的语句不起作用

转载 作者:行者123 更新时间:2023-11-28 04:47:18 24 4
gpt4 key购买 nike

由于某种未知的原因,checkID 函数中的 if 语句已停止工作,并且无论输入什么输入都仅显示“OK”。我不太确定我做错了什么,但有人可以看一下我的代码吗?目前,代码似乎只运行函数中的最后一个 if 语句,并且只运行最后一条消息“Ok”。

这是我的 HTML:

function begin() {
var Name = document.forms["Cars"]["Name"].value;
var NameID = document.forms["Cars"]["NameID"].value;
var IDName = "";
var PersonID = "";

var checklength = Name.length;
CheckName();
console.log(checklength, checklength < 100)
var checkIDlength = NameID.length;
checkID();
console.log(checkIDlength, checkIDlength < 100)
return false;


function CheckName() {
if (checklength > 0 && checklength < 21) {
IDName = "Ok"
} else {
IDName = "Invalid Name Length";
}

document.getElementById('IDName').innerHTML = IDName;
return false;
}

function checkID() {
if (checkIDlength != 6) {
PersonID = "Invalid ID Length"
}
document.getElementById('PersonID').innerHTML = PersonID

if (NameID.indexOf('BR') === 0) {} else {
PersonID = "Invalid ID Type"
document.getElementById('PersonID').innerHTML = PersonID

}
if (isNaN(NameID.Length != 2)) {
PersonID = "The Last 2 Digits must be numeric";
} else {
PersonID = "OK"
document.getElementById('PersonID').innerHTML = PersonID
}
}
}
<!DOCTYPE html>
<html>

<body>
<form name="Cars">
<h1>Car Sales</h1>
<p>Enter Name</p>
<input type="text" style="text-transform: uppercase" name="Name"><br>
<p id="IDName"></p>
<p>Enter ID</p>
<input type="text" name="NameID"><br>
<p id="PersonID"></p>
<br>
<button onclick="return begin()">Calculate</button>
</form>
</body>

最佳答案

看起来我已经通过在 IDCheck 函数的 if 语句中添加一些 return false 来完成这项工作,我使用“BR1231”作为测试用例,我不确定这是否 100% 有效,但这里是我的代码:

    function begin() {
var Name = document.forms["Cars"]["Name"].value;
var NameID = document.forms["Cars"]["NameID"].value;
var IDName = "";
var PersonID = "";

var checklength = Name.length;
CheckName();
console.log(checklength, checklength < 100)
var checkIDlength = NameID.length;
checkID();
console.log(checkIDlength, checkIDlength < 100)
return false;


function CheckName(){
if (checklength >0 && checklength <21)
{
IDName = "Ok"
}
else{
IDName = "Invalid Name Length";
}

document.getElementById('IDName').innerHTML = IDName;
return false;
}


function checkID(){
if (checkIDlength !=6)
{
PersonID = "Invalid ID Length"

document.getElementById('PersonID').innerHTML = PersonID
return false;
}

if (NameID.indexOf('BR') === 0) {
}
else {
PersonID = "Invalid ID Type"
document.getElementById('PersonID').innerHTML = PersonID
return false;

}
if (isNaN(NameID.Length !=2))
{
PersonID = "The Last 2 Digits must be numeric";
return false;

}
else{
PersonID = "OK"
document.getElementById('PersonID').innerHTML = PersonID

}

}
}

关于javascript - 如果函数中的语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246234/

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