简体   繁体   English

仅当在输入字段中键入某个单词时,如何启动javascript:alert框?

[英]How do I initiate a javascript:alert box only if a certain word is typed into the input field?

<script>
var monster = 40
var damage = Math.floor(Math.random()*10)
</script>


<p>Type 'Spark' or 'Fire' to Attack.</p>
<form action="javascript:alert(  'Enemy has' + (monster- damage) + 'Health!' );"
>
  <div>
    <input type="text">
    <input type="submit">
  </div>

</form>
<span></span>


</body>

So this is my code so far I type anything in it gives me a text box saying the Enemy has whatever health, I would like to have it where only if you type spark or fire into the input field you get that text box and if you type something random like skdfslkfha nothing happens (unlike now :/) 所以到目前为止,这是我的代码,我在其中输入任何内容都会给我一个文本框,说明敌人具有任何健康状况,我希望将其放置在只有在输入字段中键入spark或fire的情况下,您才能得到该文本框,并且如果像skdfslkfha这样的随机输入什么都不会发生(与现在不同:/)

JS Fiddle JS小提琴

html HTML

<form action="javascript:ale()">
    <div>
        <input type="text" id="find">
        <input type="submit">
    </div>
</form>

javascript JavaScript的

function ale() {

    a = document.getElementById("find").value
    if (a == 'Spark' || a == 'Fire') {
        var monster = 40
        var damage = Math.floor(Math.random() * 10)
        alert('Enemy has ' + (monster - damage) + ' Health!');
    }
    else{
        alert('worng keyword');
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM