1

我已经使用 css 解决了这个问题,但是我不允许使用 css 将文本大写为用户类型,然后在提交之前将提交的数据更改为大写?任何人都可以使用 jquery/JS 帮助我解决这个问题。

4

4 回答 4

3

keyup event。只需应用转换为大写的方法

this.value.toUpperCase();

检查小提琴

但是,如果您希望文本在提交数据之前为大写。那么最好将其转换为大写一次,而不是将其绑定在keyup event..

// submit here
      var data = $('#input')[0].value.toUpperCase();

更改事件小提琴

$("input").on('keyup', function(e) {
    if( [8,37,38].indexOf(e.keyCode) > -1 ) {
        return;
    }
    this.value = this.value.toUpperCase();
});

另一个小提琴

您可以使用键码并检查是否按下了退格键或箭头键。否则大写或返回它。

于 2013-06-26T17:41:25.110 回答
1

您可以使用keyup事件:

$("#idOfInput").keyup(function() {
    this.value = this.value.toUpperCase();
});

演示:http: //jsfiddle.net/Sqef8/

于 2013-06-26T17:41:52.753 回答
1

您可以在 keydown 事件中捕获 a 到 z 并用 AZ 替换选定的文本:

    function ucase(event, source) {
        var evt = event ? event : window.event;
        if (evt.keyCode >= 65 && evt.keyCode <= 90) {
            textboxReplaceSelect(source, String.fromCharCode(evt.keyCode).toUpperCase());
            return false;
        }
        return true;
    }

    function textboxReplaceSelect(oTextbox, sText) {
        var isOpera = navigator.userAgent.indexOf("Opera") > -1;
        var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
        var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;
        if (isIE) {
            var oRange = document.selection.createRange();
            oRange.text = sText;
            oRange.collapse(true);
            oRange.select();
        } else if (isMoz) {
            var iStart = oTextbox.selectionStart;
            oTextbox.value = oTextbox.value.substring(0, iStart) + sText + oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);
            oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);
        }

        oTextbox.focus();
    }

如何调用它的示例:

    <input type="text" onkeydown="return ucase(event,this);" />

http://jsfiddle.net/YbD2N/4/

于 2013-06-26T17:57:48.380 回答
0

在 keyup 功能上,文本框是大写的,但在编辑光标位置时,在大多数浏览器中总是强制结束。它就像一个魅力

$(selector).on('keyup',function(){
   var start = this.selectionStart,
       end = this.selectionEnd;     
       this.value = this.value.toUpperCase();
       this.setSelectionRange(start, end);
});
于 2013-06-26T18:52:29.947 回答