簡體   English   中英

如何使輸入不區分大小寫

[英]How to make an input not case sensitive

我想知道當用戶在輸入中輸入內容時如何使其不必與document.getElementById("spanId").innerHTML = "146.5 meters";的值相同大小寫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";
    }
}

首先,對輸入字段使用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())

暫無
暫無

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

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