2017-03-03 23 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

相关问题