如何解决写入 TextField 后立即从它
我正在尝试在用户输入内容后立即更新 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;
}
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。