Replace several input values

2019-08-02 13:47发布

I have this function for a small chat system that I'm working on. When the users types "shout" preceded by some text (ex: "shout hello!"). I want only the "hello!" to output (the rest should be ignored). This worked fine when I only had one command. But now I have "shout" & "message". I tried doing something with the code below but it doesn't seem to work:

function checkValue() {
var value = document.getElementById("thisinput").value;
  // output text without "message" in front
  if (value == "message") {
  $('#output').html(value.replace('message', ''));  
  }
  // output text without "shout" in front
  else if (value == "shout") {
  $('#output').html(value.replace('shout', ''));  
  }
  // output nothing
}

Thanks!

1条回答
闹够了就滚
2楼-- · 2019-08-02 14:26

try

if(value.indexOf("message") != -1)
查看更多
登录 后发表回答