无法利用javascript / jquery对输入中的第一个单词进行大写 [英] Trouble capitalizing first word in input with javascript/jquery
本文介绍了无法利用javascript / jquery对输入中的第一个单词进行大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经通过这个问题浏览了很多其他线程。我有问题将输入的第一个字母大写。
I have looked through a lot of the other threads on here with this question. I am having issues capitalizing the first letter of an input.
我似乎无法获得 substr()
功能正常工作。它总是返回整个字符串,而每个单词都大写。
I cannot seem to get the substr()
function to work properly. It always returns the whole string with each word capitalized instead.
function crmForm_lastname_onblur(sourceField, sourceRowId) {
var x=document.getElementById("crmForm_lastname");
x.value=x.value.toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.substr(0,1).toUpperCase()+letter.substr(1);
});
console.log(x.value);
}
任何帮助将不胜感激。我不是一个Javascript专家,但我可以理解代码在某些方面做了什么。谢谢。
Any help would be appreciated. I am no means a Javascript expert but I can understand what the code is doing in certain areas. Thank you.
推荐答案
您是否尝试这样做
function crmForm_lastname_onblur(sourceField, sourceRowId) {
var val = sourceField.value;
if (val.charAt(0).toUpperCase() != val.charAt(0))
sourceField.value = val.charAt(0).toUpperCase() + val.slice(1)
}
这篇关于无法利用javascript / jquery对输入中的第一个单词进行大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文