简体   繁体   English

即使条件为true,在HTML中使用Javascript也会返回false

[英]Using Javascript in HTML returning false even if the condition is true

I'm using Javascript in HTML to return a popup error if the input in the searchbar is not 'Dog'. 如果搜索栏中的输入不是“ Dog”,我正在HTML中使用Javascript返回弹出错误。 However, it still shows the popup error when the input is 'Dog'. 但是,当输入为“ Dog”时,它仍然显示弹出错误。

 <div class="search" >

    <input type="text" name="s" 
      <a href="/redirect.php">
    <button id="find" type="submit" 
    onclick="javascript: if(document.getElementById('find').value!='Dog') 
    {
        alert('This animal does not exist in the system, try another one...');
        return false;
    }">&nbsp;</button>
  </div>

The code is supposed to go to redirect.php if the input in the searchbar is 'dog'. 如果搜索栏中的输入为“ dog”,则该代码应转到redirect.php。 Where am I going wrong with the code? 代码哪里出问题了?

try this 尝试这个

<div class="search" >
    <input type="text" id="find" name="s" />
    <a href="/redirect.php">
    <button  type="submit" 
    onclick="javascript: if(document.getElementById('find').value!='Dog') {
        alert('This animal does not exist in the system, try another one...');
        return false;
    }">&nbsp;</button>
</div>

Update: 更新:

The reason why it wasn't working before was because the button element had the find id instead of the text field. 之所以不能使用,是因为button元素具有查找 ID而不是文本字段。

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

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