Javascript大写不起作用

Lea*_*ack 2 javascript jquery

我试图在用户填写后将文本输入值更改为大写字符串:

$('#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">
Run Code Online (Sandbox Code Playgroud)

如果我在场上写"leandro",则alert(uni)抛出:"leandro"而不是"LEANDRO".任何的想法?

我无法使用CSS,因为我必须通过此表单发送大写数据,而css只更改呈现而不是值

cli*_*ity 17

改为:

uni = uni.toUpperCase();
Run Code Online (Sandbox Code Playgroud)

字符串是不可变的,因此您需要将结果分配给变量,覆盖旧字符串.

  • 好答案.你的用户名/头像让我笑了大约5分钟. (2认同)

Poi*_*nty 6

.toUpperCase()函数返回一个新字符串; 它不会修改现有的字符串.