2017-03-03 58 views
0

我使用JavaScript並嘗試使用java腳本/ j查詢註冊表單,當某些用戶輸入某個單詞時,將自動禁用下一個輸入字段。 :在該領域用戶鍵入「麪包」,那麼下一個字段變爲空白,也不能使用 我已經嘗試搜索它的搜索引擎,但只有約禁用提交button.Below答案是我的代碼:Javascript:禁用textfield當其他字段匹配某個單詞

<script> 
    function matchWord(e){ 
    console.log(document.getElementById('food').value.trim()); 
    if(document.getElementById('Food').value.trim() == "Bread"){ 
    document.getElementsById('OFood')disabled==true; 
    document.getElementsById('PFood')disabled==true; 
     } 
    } 

    document.getElementById('food').onkeyup = matchWord; 
    </script> 

這是我的HTML:

請插入其他食物,然後麪包

Food:<input type = "text" name="food" id="food" value ="" size ="22" placeholder ="Enter your Favourite food"/><br> 
    Drink:<input type = "text" name="Ofood" id="Ofood" value="" size="22" placeholder="Enter your Favourite Drink"/><br>` 
    Restaurant:<input type = "text" name="Pfood" id="Pfood" value="" size="22" placeholder="name your best restaurant"/> 

我也嘗試運行代碼,但它不工作。我需要一個解決方案。 這都謝謝。

回答

1

元素ID是區分大小寫的。你也有一些語法錯誤。我已經固定的代碼爲您提供:

<script> 
    function matchWord(e){ 
     if(document.getElementById('food').value.trim() === "Bread"){ 
      document.getElementById('Ofood').disabled = true; 
      document.getElementById('Pfood').disabled = true; 
     } 
    } 

    document.getElementById('food').onkeyup = matchWord; 
</script> 

這裏測試:https://jsfiddle.net/3pd40ep4/

+0

謝謝,它的工作。但有沒有其他方法可以使其不區分大小寫? – Nexz

+1

@Nexz,不,它不能不區分大小寫。它以這種方式設計來唯一標識元素。 – Samir

+0

@Samir謝謝你的迴應。 – Nexz

0

很多的錯字在你的腳本

function matchWord(e){ 
 
    console.log(document.getElementById('food').value.trim()); 
 
    if(document.getElementById('food').value.trim() == "Bread"){ 
 
    document.getElementById('Ofood').disabled=true; 
 
    document.getElementById('Pfood').disabled=true; 
 
     } 
 
    } 
 

 
    document.getElementById('food').onkeyup = matchWord;
Food:<input type = "text" name="food" id="food" value ="" size ="22" placeholder ="Enter your Favourite food"/><br>  Drink:<input type = "text" name="Ofood" id="Ofood" value="" size="22" placeholder="Enter your Favourite Drink"/><br>`  Restaurant:<input type = "text" name="Pfood" id="Pfood" value="" size="22" placeholder="name your best restaurant"/>

+0

如果我想使用類名稱,我可以使用與byId相同的語法嗎?我的意思是像這樣document.getElementByClassName(「OFood」)。disabled = true; – Nexz

+0

是的,你可以...但事情是...不同的元素可以有相同的類名...但是ID是唯一的 – Dherya

+0

如果ID有另一個功能,我們可以再次使用它作爲其他功能嗎? – Nexz

相關問題