【问题标题】:.includes method is always true in js [closed].includes 方法在 js 中始终为真 [关闭]
【发布时间】:2020-05-21 19:01:13
【问题描述】:

我有这个小代码来使用需要包含各种字符串的密钥进行登录...但是 .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">

【问题讨论】:

  • keyRequireBoolean0 = true 设置true,改为使用==进行比较。
  • UHHHHHHH 是的,我忘了添加另一个 =
  • 你什么都不需要,只要if(keyRequireBoolean0)

标签: javascript html include


【解决方案1】:

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

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');
}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多