2015-09-05 49 views
-1

我想知道如何使用戶輸入某些內容時,如果輸入的內容不必與document.getElementById("spanId").innerHTML = "146.5 meters";出現的值相同。如何使輸入不區分大小寫

HTML

<input id="ask" type="text" 
     placeholder = "Ex: how tall is the Gateway Arch" 
     onfocus="this.placeholder = ''" 
     onblur="this.placeholder = 'Ex: how tall is the gateway arch'" 
     text-align: center/> 

JS

document.getElementById("button").onclick = function() { 
    if (document.getElementById("ask").innerHTML == "how tall are the pyramids") { 
     document.getElementById("spanId").innerHTML = "146.5 meters"; 
    } else if (document.getElementById("ask").value == "how tall is the gateway arch") { 
     document.getElementById("spanId").innerHTML = "630 feet"; 
    } else if (document.getElementById("ask").value == "how tall is the empire state building") { 
     document.getElementById("spanId").innerHTML = "1,454 feet"; 
    } 
} 

回答

1

首先,使用value,而不是innerHTML輸入框的

,轉換兩側小寫大寫比較

嘗試這樣

var ask=document.getElementById("ask").value.toLowerCase(); 
if (ask =="how tall are the pyramids") 

期間或者這樣

var ask=document.getElementById("ask").value.toUpperCase(); 
if (ask =="how tall are the pyramids".toUpperCase()) 
+0

這將只對如果else語句不是別人 –

+0

工作分配給它一個變量在任何需要的地方使用該變量而不是每次使用'document.getElementById(「ask」).value' –

+0

這是一個愚蠢的問題,但你將如何分配一個變量 –