简体   繁体   English

如何将输入框中键入的文本转换为小写?

[英]How can I convert text typed in an input box to lower case?

我不知道如何将键入到文本输入框(txtQuestion)的文本转换为全部小写,即键入“ input”或“ INpUt”将被读取并输出相同的结果。

Use toLowerCase() function. 使用toLowerCase()函数。 Eg: "INPuT".toLowerCase(); 例如: "INPuT".toLowerCase();

 exampleFunction = function(){ //First we get the value of input var oldValue = document.getElementById('input').value; //Second transform into lowered case var loweredCase = oldValue.toLowerCase(); //Set the new value of input document.getElementById('input').value = loweredCase; } 
 <input id="input" type="text" onkeyup="exampleFunction()" /> 

You have to choose the events that you want to watch and then return to the input the same value but lowercase: 您必须选择要观看的事件,然后将相同值但小写的返回到输入:

 function InputLowerCaseCtrl(element, event) { console.log(element.value) return element.value = (element.value || '').toLowerCase(); }; InputLowerCaseCtrl.bindTo = ['blur'].join(' '); document.addEventListener('DOMContentLoaded', function() { var input = document.querySelector('.input-lowercase'); return input.addEventListener(InputLowerCaseCtrl.bindTo, InputLowerCaseCtrl.bind(this, input)); }); 
 <input class="input-lowercase" type="text" /> 

You can convert any string input to lowercase using the String.prototype.toLowerCase function 您可以使用String.prototype.toLowerCase函数将任何字符串输入转换为小写

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase

For you example with an input for txtQuestion. 对于您而言,带有txtQuestion输入的示例。

 var inputStringLowerCase = inputTxtQuestion.value.toLowerCase(); 

https://jsfiddle.net/fbkq2po4/ https://jsfiddle.net/fbkq2po4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM