问题描述
我正在尝试在用户输入内容后立即更新 TextField。我喜欢避免人们以不同的文本格式书写但具有适当的大写字母,例如。在这种情况下,文本需要更新 (firstCapitalLetter) John,而不是 jOHN 或 JOHN 或 john。
我尝试复制此处描述的方法:Format input text to uppercase,但我未能使其适应我的情况。 txtFirstName
是 TextField 的 ID。
NWF$(document).ready(function () {
NWF$("#" + txtFirstName).change(function () {
// get the text value from the FirstNname textfield
var textContent = NWF$('#' + txtFirstName).text();
// check to see if the value is not null
if (textContent.length > 0) {
// format the first letter to UpperCase and the rest to LowerCase
textContent = NWF$("#" + txtFirstName).toupperCase() + txtFirstName.substr(1).toLowerCase();
NWF.value = textContent;
}
});
});
解决方法
经过多次试验,我设法让它工作起来。
现在,txtFirstName
会根据需要更改格式 :)
NWF$(document).ready(function () {
NWF$("#" + txtFirstName).change(function () {
// get the text value from the FirstNname textfield
var textContent = this.value;
// check to see if the value is not null
if (textContent.length > 0) {
// format the first letter to UpperCase and the rest to LowerCase
textContent = textContent[0].toUpperCase() + textContent.substr(1).toLowerCase();
this.value = textContent;
}
});
});
,如果您必须更新两个单独的 txtFields
,这是另一种选择。
也许你们中的一个人可以提出一个更好的方法。
基本上,我想说的是,当 txtFields 更新时,它们需要被格式化为正确的大写,以避免出现类似:john / JOHN / jOHn 的情况
NWF$(document).ready(function () {
NWF$("#" + txtFirstName).change(function () {
// get the text value from the FirstNname textfield
var textContent = this.value; //alert(textContent)
// check to see if the value is not null
if (textContent.length > 0) {
// format the first letter to UpperCase and the rest to LowerCase
textContent = textContent[0].toUpperCase() + textContent.substr(1).toLowerCase();
this.value = textContent;
}
});
NWF$("#" + txtLastName).change(function () {
// get the text value from the FirstNname textfield
var textContent = this.value; //alert(textContent)
// check to see if the value is not null
if (textContent.length > 0) {
// format the first letter to UpperCase and the rest to LowerCase
textContent = textContent[0].toUpperCase() + textContent.substr(1).toLowerCase();
this.value = textContent;
}
});
});