最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript uppercase not working - Stack Overflow

programmeradmin3浏览0评论

I am trying to change text input value after user fill it, to uppercase string:

$('#University').blur(function(){
    if ( $(this).attr("value") != '') {
        var uni = $(this).val();
        uni.toUpperCase();
        alert(uni);
        $(this).attr("value", uni);
        };
});

<input type="text" name="Universidad" size="45" class="required" id="University">

If I write "leandro" into the field, the alert(uni) throws: "leandro" and not "LEANDRO". Any idea?

I could not use CSS because I must send uppercase data through this form, and css only change the rendering and not the value

I am trying to change text input value after user fill it, to uppercase string:

$('#University').blur(function(){
    if ( $(this).attr("value") != '') {
        var uni = $(this).val();
        uni.toUpperCase();
        alert(uni);
        $(this).attr("value", uni);
        };
});

<input type="text" name="Universidad" size="45" class="required" id="University">

If I write "leandro" into the field, the alert(uni) throws: "leandro" and not "LEANDRO". Any idea?

I could not use CSS because I must send uppercase data through this form, and css only change the rendering and not the value

Share Improve this question asked May 21, 2012 at 19:42 Leandro CusackLeandro Cusack 3691 gold badge6 silver badges21 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 16

Change to this:

uni = uni.toUpperCase();

Strings are immutable, so you need to assign the result to the variable, overwriting the old string.

The .toUpperCase() function returns a new string; it doesn't modify the existing string.

You could rewrite your function in a better way. See below,

$('#University').blur(function(){
    this.value = this.value.toUpperCase();
});

toUpperCase() is a method. It returns a value that you need to do something with.

You called the .toUpperCase() method on uni, but you didn't assign it anywhere.

uni = uni.toUpperCase();

... should help.

发布评论

评论列表(0)

  1. 暂无评论