键盘的大写字母如何锁定文本框的第一个字母?

时间:2015-05-01 07:50:26

标签: javascript jquery html css cordova

我正在开发一个cordova应用程序。对于输入文本,我想为第一个字母启用键盘大写锁定键。以前我使用text-form:capitalize但是如果他想要第一个字母小写,则用户不能再将第一个字母更改为小写字母(大写字母属性不能在键盘上启用大写锁定键)。

我只需要在键入第一个字母时按下大写锁定键,如果用户禁用它,那么他可以输入小写字母作为小写字母。

任何css,html,jquery,javascript解决方案的家伙?我的应用程序中有多个文本字段,我希望该解决方案适用于所有输入类型=“text”。

我在下面输入的示例文本字段:

java.lang.OutOfMemoryError: Java heap space
at com.android.dx.rop.code.RegisterSpecSet.<init>(RegisterSpecSet.java:49)
at com.android.dx.rop.code.RegisterSpecSet.mutableCopy(RegisterSpecSet.java:383)
at com.android.dx.ssa.LocalVariableInfo.mutableCopyOfStarts(LocalVariableInfo.java:168)
at com.android.dx.ssa.LocalVariableExtractor.processBlock(LocalVariableExtractor.java:103)
at com.android.dx.ssa.LocalVariableExtractor.doit(LocalVariableExtractor.java:89)
at com.android.dx.ssa.LocalVariableExtractor.extract(LocalVariableExtractor.java:55)
at com.android.dx.ssa.SsaConverter.convertToSsaMethod(SsaConverter.java:49)
at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:98)
at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:72)
at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:299)
at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:139)
at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:94)
at com.android.dx.command.dexer.Main.processClass(Main.java:682)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
at com.android.dx.command.dexer.Main.access$600(Main.java:78)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:596)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:188)
at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:786)
at com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:597)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:726)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)

1 个答案:

答案 0 :(得分:0)

您无法直接打开大写锁定或Shift键。

您可以捕获按键事件,然后,如果该字段有一个字符,请将其设为大写。那么问题是避免弄乱用户的光标位置。在大多数现代浏览器中,您可以通过在更新字段后设置插入位置来实现。如果用户不希望上限,则他们必须返回并编辑,这不是理想的。

我不会在一般网页(如果有的话)中这样做,有太多的浏览器在那里进行测试以避免让用户不满意的问题,但是如果你的情况更有限并且可以充分测试使用目标浏览器,这可能是可行的。

以下是适用于当前Chrome,Firefox,IE和iOS Safari的示例:

&#13;
&#13;
$("#testing").on("keypress", function(e) {
  // Only do it if the field is blank before the keypress
  // (the event occurs before the character is added to
  // the field)
  if (this.value.length === 0) {
    setTimeout(maybeCap.bind(this), 0);
  }
});

function maybeCap() {
  // Only do it if the field now has a single character
  if (this.value.length == 1) {
    this.value = this.value.toUpperCase();
    setCaretPosition(this, 1);
  }
}

function setCaretPosition(elem, caretPos) {
  if (elem) {
    if (elem.createTextRange) {
      var range = elem.createTextRange();
      range.move('character', caretPos);
      range.select();
    } else {
      if (elem.selectionStart) {
        elem.focus();
        elem.setSelectionRange(caretPos, caretPos);
      } else
        elem.focus();
    }
  }
}
&#13;
<input type="text" id="testing">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

或者作为jQuery插件:

&#13;
&#13;
$.fn.capFirst = function() {
  this.on("keypress", keypressHandler);
  
  function keypressHandler(e) {
    // Only do it if the field is blank before the keypress
    // (the event occurs before the character is added to
    // the field)
    if (this.value.length === 0) {
      setTimeout(maybeCap.bind(this), 0);
    }
  }
  
  function maybeCap() {
    // Only do it if the field now has a single character
    if (this.value.length == 1) {
      this.value = this.value.toUpperCase();
      setCaretPosition(this, 1);
    }
  }

  function setCaretPosition(elem, caretPos) {
    if (elem) {
      if (elem.createTextRange) {
        var range = elem.createTextRange();
        range.move('character', caretPos);
        range.select();
      } else {
        if (elem.selectionStart) {
          elem.focus();
          elem.setSelectionRange(caretPos, caretPos);
        } else
          elem.focus();
      }
    }
  }
};

$("input[type=text]").capFirst();
&#13;
<div><input type="text"></div>
<div><input type="text"></div>
<div><input type="text"></div>
<div><input type="text"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;