簡體   English   中英

即使條件為true,在HTML中使用Javascript也會返回false

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

如果搜索欄中的輸入不是“ Dog”,我正在HTML中使用Javascript返回彈出錯誤。 但是,當輸入為“ 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>

如果搜索欄中的輸入為“ dog”,則該代碼應轉到redirect.php。 代碼哪里出問題了?

嘗試這個

<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>

更新:

之所以不能使用,是因為button元素具有查找 ID而不是文本字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM