。includes方法在js中始终为true

问题描述 投票:-1回答:1

我有这个小代码,用需要包含各种字符串的键进行登录...但是.includes方法始终为真,有人可以向我解释这是怎么回事吗?

function validate() {
var keyInput = document.getElementById("keyInputBox").value;
console.log('User trying to log with key: "' + keyInput + '"');
var keyRequireBoolean0 = keyInput.includes('anon')
if (keyRequireBoolean0 = true) {
    var keyRequireBoolean1 = keyInput.includes('sup')
    if (keyRequireBoolean1 = true) {
        alert('login successfull ' + keyRequireBoolean0)
    } else {
        alert('Invaild Key');
    }
} else {
    alert('Invaild Key');
}}

这是html部分:

  <input type="password" id="keyInputBox" placeholder="Key">
  <input type="submit" onclick="validate()" value="Check">
javascript html include
1个回答
0
投票

您想要做的是比较布尔值:

function validate() {
var keyInput = document.getElementById("keyInputBox").value;
console.log('User trying to log with key: "' + keyInput + '"');
var keyRequireBoolean0 = keyInput.includes('anon')
if (keyRequireBoolean0 === true) {
    var keyRequireBoolean1 = keyInput.includes('sup')
    if (keyRequireBoolean1 === true) {
        alert('login successfull ' + keyRequireBoolean0)
    } else {
        alert('Invaild Key');
    }
} else {
    alert('Invaild Key');
}}
© www.soinside.com 2019 - 2024. All rights reserved.