-1

我正在尝试使用 Javascript 在页面重定向之前验证用户输入。用户可以输入学生 ID、姓名或性别,根据他们的输入,他们将被重定向到一个 URL。

但是,我似乎没有在我的 javascript 中正确获取多个条目,并且单击提交按钮时没有任何反应。

我尝试了在这里找到的不同解决方案。

请参阅下面的 JavaScript 代码;

var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate(){
var username = document.getElementById("studentid").value;

if ( studentid == "12345" || studentid == "Daniel" || studentid == "Boy"){
alert ("Correct Input");
window.location = "https://www.google.com"; 
// Redirecting to other page.

return false;
}

else{
attempt --;// Decrementing by one.
alert("ATTENTION!\nInvalid student ID!\nNot associated with any student\nYou have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("studentid").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}

我尝试使用以下解决方案;

if ( studentid == "#12345, #Daniel, #Boy"));{
alert ("correct input");
window.location = "https://www.google.com"; 
// Redirecting to other page.


if ( studentid == '12345', 'Daniel', 'Boy'){
alert ("correct input");
window.location = "https://www.amazon.com"; 
// Redirecting to other page.

4

1 回答 1

0

经过这么多尝试,我终于做对了!

var attempt = 3;
function check(form)
{
if(form.studentid.value == "12345" || form.studentid.value == "DANIEL" || form.studentid.value == "BOY")
  {
    window.location.replace('https://www.google.com')

return false;
}
 else
 {
   attempt --;// Decrementing by one.
alert("ATTENTION!\nInvalid student ID!\nNot associated with any student!\nYou have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("studentid").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}

于 2022-02-03T14:52:14.677 回答