大写文本输入的第一个字符
Capitalise the first character of text input
我看不出哪里出了问题。我的代码是...
function firstC()
{
var x = document.getElementsByClassName("uValue");
for(var i = 0; i < x.length; i++) {
x.value.charAt(0).toUpperCase();
}
}
它被...调用
<td><input type="text" name="firstname" value="(required)" id="firstName" class="uValue" onclick="empty(this.id)" onblur="firstC()" /></td>
如果输入框的值为“(必填)”,empty()
函数可以通过删除输入框的值来正常工作,但我无法让 firstC()
函数将任何输入框的第一个字符大写输入。
编辑:我正在使用 getElementsByClassName
,因为有多个输入框,我试图允许它们使用相同的功能。
您应该改为将 this
传递到您的内联 JS 事件器中:
onclick="empty(this)" onblur="firstC(this)"
例子
function empty(el) { // el now refers to the this referrer
el.value="";
}
function firstC(el) {
var val = el.value;
el.value = val.charAt(0).toUpperCase() + val.substr(1);
}
<input type="text" onclick="empty(this)" onblur="firstC(this)" value="(required)" id="firstName" class="uValue" name="firstname">
P.S:不要忘记,您可以简单地使用 placeholder
属性
而不是使用 empty()
和 value="(required)"
placeholder="(required)"
您忘记重新赋值
function firstC()
{
var x = document.getElementsByClassName("uValue");
for(var i = 0; i < x.length; i++) {
x[i].value = x[i].value.charAt(0).toUpperCase() + x[i].value.substr(1);
}
}
我看不出哪里出了问题。我的代码是...
function firstC()
{
var x = document.getElementsByClassName("uValue");
for(var i = 0; i < x.length; i++) {
x.value.charAt(0).toUpperCase();
}
}
它被...调用
<td><input type="text" name="firstname" value="(required)" id="firstName" class="uValue" onclick="empty(this.id)" onblur="firstC()" /></td>
如果输入框的值为“(必填)”,empty()
函数可以通过删除输入框的值来正常工作,但我无法让 firstC()
函数将任何输入框的第一个字符大写输入。
编辑:我正在使用 getElementsByClassName
,因为有多个输入框,我试图允许它们使用相同的功能。
您应该改为将 this
传递到您的内联 JS 事件器中:
onclick="empty(this)" onblur="firstC(this)"
例子
function empty(el) { // el now refers to the this referrer
el.value="";
}
function firstC(el) {
var val = el.value;
el.value = val.charAt(0).toUpperCase() + val.substr(1);
}
<input type="text" onclick="empty(this)" onblur="firstC(this)" value="(required)" id="firstName" class="uValue" name="firstname">
P.S:不要忘记,您可以简单地使用 placeholder
属性
empty()
和 value="(required)"
placeholder="(required)"
您忘记重新赋值
function firstC()
{
var x = document.getElementsByClassName("uValue");
for(var i = 0; i < x.length; i++) {
x[i].value = x[i].value.charAt(0).toUpperCase() + x[i].value.substr(1);
}
}