将输入更改为大写

Jsh*_*hee 63 javascript jquery

JS:

<script type="text/css">
$(function() {
    $('#upper').keyup(function() {
        this.value = this.value.toUpperCase();
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)

HTML

<div id="search">
        <input type="radio" name="table" class="table" value="professor" tabindex="1" /> Professor
        <input type="radio" name="table" class="table" value="department" tabindex="2" /> Department
        <input type="radio" name="table" id="upper" class="table" value="course" tabindex="3" /> Course
        <input type="text" name="search" class="keywords" value="Select an option..." onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?':this.value;" tabindex="4"  />
        <div id="content"> </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

为什么这还不行?只是试图从JS调用div".keywords".

Tim*_*Tim 233

我认为最优雅的方式是没有任何JavaScript但使用CSS.你可以使用text-transform: uppercase(这是内联只是为了这个想法):

<input id="yourid" style="text-transform: uppercase" type="text" />
Run Code Online (Sandbox Code Playgroud)

编辑:

因此,在您的情况下,如果您希望关键字为大写更改: keywords: $(".keywords").val(),to$(".keywords").val().toUpperCase(),

  • 需要记住的是,使用(只)JavaScript检索值将返回原始文本; 即不是大写.[这里的例子](http://jsfiddle.net/Shaz/NWS6J/2/) (34认同)
  • 如果你只是想让它看起来大写,那么这可能是真的.但是,如果您希望实际的input.value为大写,则必须在提交表单之前复制您的工作,或者使用input.value,这使得它不那么优雅. (22认同)
  • 用户输入真的是大写还是只是在这里显示? (8认同)
  • 这个简单的想法让我远离了事件互动女巫酿造地狱的火热深处.愿系统风扇始终在你的背后,愿散热片在你的脸上闪耀. (5认同)
  • @Tim - 你不允许有INPUT的结束标记:http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT (2认同)

Lig*_*ica 48

Javascript字符串对象具有toLocaleUpperCase()使转换本身变得容易的功能.

这是一个实时大写的例子:

$(function() {
    $('input').keyup(function() {
        this.value = this.value.toLocaleUpperCase();
    });
});
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会完全重置文本框内容,因此用户的插入位置(如果不是"文本框的末尾")将丢失.

不过,你可以通过一些浏览器切换魔法来破解它:

// Thanks http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
function getCaretPosition(ctrl) {
    var CaretPos = 0;    // IE Support
    if (document.selection) {
        ctrl.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
        CaretPos = ctrl.selectionStart;
    }

    return CaretPos;
}

function setCaretPosition(ctrl, pos) {
    if (ctrl.setSelectionRange) {
        ctrl.focus();
        ctrl.setSelectionRange(pos,pos);
    }
    else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}


// The real work

$(function() {
    $('input').keyup(function() {
        // Remember original caret position
        var caretPosition = getCaretPosition(this);

        // Uppercase-ize contents
        this.value = this.value.toLocaleUpperCase();

        // Reset caret position
        // (we ignore selection length, as typing deselects anyway)
        setCaretPosition(this, caretPosition);
    });
});
Run Code Online (Sandbox Code Playgroud)

最终,伪造它可能是最容易的.text-transform: uppercase在文本框上设置样式,使其对用户显示为大写,然后在Javascript中,只要用户的插入符焦点完全离开文本框,就应用文本转换:

HTML:

<input type="text" name="keywords" class="uppercase" />
Run Code Online (Sandbox Code Playgroud)

CSS:

input.uppercase { text-transform: uppercase; }
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

$(function() {
    $('input').focusout(function() {
        // Uppercase-ize contents
        this.value = this.value.toLocaleUpperCase();
    });
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Ebr*_*him 9

如果您将其用于html 输入,则无需使用 JavaScript 即可轻松完成此操作!或任何其他 JS 库。使用 CSS 标签text-transform将是标准且非常容易的:

<input type="text" style="text-transform: uppercase" >
Run Code Online (Sandbox Code Playgroud)

或者您可以使用名为“text-uppercase”的引导程序类

<input type="text" class="text-uppercase" >
Run Code Online (Sandbox Code Playgroud)

这样,你的代码就简单多了!


Cro*_*uch 6

也可以这样做,但其他方式似乎更好,如果你只需要它一次就会派上用场.

onkeyup="this.value = this.value.toUpperCase();"
Run Code Online (Sandbox Code Playgroud)


BiA*_*AiB 5

尝试:

$('#search input.keywords').bind('change', function(){
    //this.value.toUpperCase();
    //EDIT: As  Mike Samuel suggested, this will be more appropriate for the job
    this.value = this.value.toLocaleUpperCase();
} );
Run Code Online (Sandbox Code Playgroud)


Blu*_*kMN 5

使用value.toUpperCase的解决方案似乎存在以下问题:在字段中键入文本会将光标位置重置为文本的末尾。使用文本转换的解决方案似乎存在以下问题:提交给服务器的文本仍可能是小写字母。此解决方案避免了这些问题:

function handleInput(e) {
   var ss = e.target.selectionStart;
   var se = e.target.selectionEnd;
   e.target.value = e.target.value.toUpperCase();
   e.target.selectionStart = ss;
   e.target.selectionEnd = se;
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" id="txtTest" oninput="handleInput(event)" />
Run Code Online (Sandbox Code Playgroud)