简体   繁体   English

当用户在 IE 浏览器中进行编辑时,防止 cursor 移动到输入框的末尾

[英]Prevent cursor from moving to end of the input box when user makes edits, in IE browser

In my anuglar 7 application, in all text input fields, I am including a method that changes the value of whatever text the user types, to uppercase.在我的 anuglar 7 应用程序中,在所有文本输入字段中,我都包含一个将用户键入的任何文本的值更改为大写的方法。 In IE browsers, when the user makes an edit to the text they enter, the cursor automatically moves to the end of the input box instead of staying in place.在 IE 浏览器中,当用户对输入的文本进行编辑时,cursor 会自动移动到输入框的末尾而不是原地不动。

I suspect the method that is causing the issue is this:我怀疑导致问题的方法是这样的:

oninput="this.value = this.value.toUpperCase()"

I do not have this issue in chrome browsers.我在 chrome 浏览器中没有这个问题。 What can I do to prevent this from happening in IE browsers?我能做些什么来防止这种情况在 IE 浏览器中发生?

My html code for the input field:我的输入字段的 html 代码:

          <input
            type="text"
            class="form-control col-lg-7"
            id="primary-email"
            formControlName="primaryEmail"
            pattern="[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?"
            placeholder="example@domain.com"
            required
            [ngClass]="{ 'is-invalid': submitted && f['primaryEmail'].errors }"
            [(ngModel)]="primaryEmail"
            oninput="this.value = this.value.toUpperCase()"
          />

As suspected, the issue was coming from正如怀疑的那样,问题来自

oninput="this.value = this.value.toUpperCase()"

The change I made to resolve this (after some googling) was to create a method in my TS file:我为解决这个问题所做的更改(经过一些谷歌搜索)是在我的 TS 文件中创建一个方法:

makeUpperCase(e) {
  const cursorStart = e.target.selectionStart;
  const cursorEnd = e.target.selectionEnd;
  e.target.value = e.target.value.toUpperCase();
  e.target.setSelectionRange(cursorStart, cursorEnd);
}

Then in the HTML file, replace the problematic line with this:然后在 HTML 文件中,将有问题的行替换为:

(input)="makeUpperCase($event)"

Hopefully this helps anyone else running into a similar issue with IE.希望这可以帮助其他遇到类似 IE 问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM