如何利用在JavaScript字符串的第一个字母? [英] How to capitalise first letter of string in javascript?

查看:144
本文介绍了如何利用在JavaScript字符串的第一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一种形式。在这种形式有10个文本框和一个按钮。我想,当用户输入文本到文本框,并失去焦点,该字符串的第一个字母应该大写。因此,我认为JavaScript是这个最好的选择。所有的onblur 10文本框的事件我要调用它。我不想叫它onbuttonclick事件。

I have created one form. In that form there are 10 textbox and one button. I would like that when the user enters text into textbox and loses focus, the first letter of the string should be capitalised. So I think javascript is best option for this. Onblur event of all 10 textbox I have to call it. I don't want to call it onbuttonclick event.

// JavaScript函数

// javascript function

function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
} 

// CS文件

// cs file

page_load()
{
string s1 = TextBox1.Text.Trim();
TextBox1.Attributes.Add("Onblur",capitaliseFirstLetter('"+s1+"'))
}

但S1始终是空的,因为我已经叫页面加载()的函数。我该如何处理呢?

But s1 is always empty because I've called the function in pageload(). How can I handle it?

推荐答案

像这样做...

page_load()
{
    TextBox1.Attributes.Add("onblur", "capitaliseFirstLetter('" + TextBox1.ClientID + "')");
}

修改如下您的JavaScript函数...

Modify your JavaScript function as below...

function capitaliseFirstLetter(txtID) {
    var txtString = document.getElementById(txtID).value;
    document.getElementById(txtID).value = txtString.charAt(0).toUpperCase() + txtString.slice(1);
} 

这篇关于如何利用在JavaScript字符串的第一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆