如何在条件下创建字符串敏感?

时间:2016-11-27 05:08:26

标签: javascript

我一直在尝试创建一个if语句,如果输入框中输入了特定的字符串,则返回一条消息。

var ram = 0;
var run = document.getElementById('run');
var r = function() {
  ram++;
  if (ram == 1) {
    var ib = document.getElementById("ib").value + ": logged in type smash to   begin hacking";
    var node = document.createElement("li");
    var textnode = document.createTextNode(ib);
    node.appendChild(textnode);
    document.getElementById("log2").appendChild(node);
  } else
  if (ib.value == "smash" && ram == 2) {
    var node2 = document.createElement("li");
    var textnode2 = document.createTextNode("smash:entering  malware_on3........making alias .....guest accessesing guest...done:)");
    node2.appendChild(textnode2);
    document.getElementById("log2").appendChild(node2);
  }
  run.addEventListener("click", r);
}

2 个答案:

答案 0 :(得分:2)

var inputBoxElement = document.getElementById("myInputBox");
if( inputBoxElement.value == "~some string~" ){
    //however you'd like to notify your users
}

不确定这是否是您正在寻找的......

答案 1 :(得分:1)

当ram不等于1时,ib将不包含任何值,因此如果语句将始终尝试读取undefined的value属性,那么第二次点击后应该看到控制台错误。< / p>

你需要做的是在第一个if语句之外选择ib,这样它就可以用在第二个if语句中:

var ram = 0;
var run = document.getElementById('run');
var r = function() {
  ram++;
  var ib = document.getElementById("ib");
  if (ram == 1) {
    var node = document.createElement("li");
    var textnode = document.createTextNode(ib.value + ": logged in type smash to   begin hacking");
    node.appendChild(textnode);
    document.getElementById("log2").appendChild(node);
  } else if (ib.value == "smash" && ram == 2) {
    var node2 = document.createElement("li");
    var textnode2 = document.createTextNode("smash:entering  malware_on3........making alias .....guest accessesing guest...done:)");
    node2.appendChild(textnode2);
    document.getElementById("log2").appendChild(node2);
  }
}
run.addEventListener("click", r);