简体   繁体   English

以编程方式在iOS设备上的输入字段中选择文本(移动版Safari)

[英]Programmatically selecting text in an input field on iOS devices (mobile Safari)

How do you programmatically select the text of an input field on iOS devices, eg iPhone, iPad running mobile Safari? 如何在iOS设备上以编程方式选择输入字段的文本,例如运行移动Safari的iPhone,iPad?

Normally it is sufficient to call the .select() function on the <input ... /> element, but this does not work on those devices. 通常,在<input ... />元素上调用.select()函数就足够了,但这对这些设备不起作用。 The cursor is simply left at the end of the existing entry with no selection made. 光标只是留在现有条目的末尾而没有做出选择。

input.setSelectionRange(0, 9999);

https://developer.mozilla.org/en/DOM/Input.select

Nothing in this thread worked for me, here's what works on my iPad: 这个帖子中没有任何东西适合我,这是我的iPad上可以使用的:

// t is the input field
setTimeout(function() {
    t.setSelectionRange(0, 9999);
}, 1);

It's hard to prove a negative, but my research suggests this is a bug in Mobile Safari. 很难证明是消极的,但我的研究表明这是Mobile Safari中的一个错误。

Note that focus() works, more or less—though it can require more than one tap to succeed, and it's not necessary if you're trying to respond to a user tap on the field in question as the tap itself will give the field focus. 请注意,focus()可以或多或少地工作 - 虽然它可能需要多次点击才能成功,并且如果您尝试响应用户点击相关字段时没有必要,因为点击本身会给出字段焦点。 Unfortunately, select() is simply non-functional in Mobile Safari. 不幸的是,请选择()简直就是移动Safari浏览器不起作用。

Your best bet may be a bug report with Apple . 您最好的选择可能是Apple的错误报告。

See this fiddle : (enter some text in the input box and click 'select text') 看到这个小提琴 :(在输入框中输入一些文字,然后点击“选择文字”)

It is selecting text in an inputbox on my iPod (5th gen iOS6.0.1), opening the keyboard and also showing the Cut/Copy/Suggest... menu 它正在iPod(第5代iOS6.0.1)的输入框中选择文本,打开键盘并显示剪切/复制/建议...菜单

Using plain javascript. 使用普通的JavaScript。 Did not try this with jQuery 没有尝试使用jQuery

document.getElementById("p1").selectionStart = 0
document.getElementById("p1").selectionEnd = 999

Note that the number 999 is just a sample. 请注意,数字999只是一个示例。 You should set these numbers to the number of characters you want to select. 您应该将这些数字设置为您要选择的字符数。

UPDATE: 更新:

  • iPod5 - iOS6.0.1 - Working ok. iPod5 - iOS6.0.1 - 正常工作。
  • iPad1 - iOS5.1.1 - Only text selected. iPad1 - iOS5.1.1 - 仅选择文本。 Tap selection once to open Cut/Copy menu 点击选择一次以打开剪切/复制菜单
  • iPad2 - iOS4.3.3 - Only text selected. iPad2 - iOS4.3.3 - 仅选择文本。 Tap selection once to open Cut/Copy menu 点击选择一次以打开剪切/复制菜单

For the last two, you might experiment by triggering a click event on the input element 对于最后两个,您可以通过在input元素上触发click事件来进行实验

UPDATE: (07-10-2013) 更新: (07-10-2013)

  • iPod5 - iOS7.0.2 - Using the fiddle in the link: Can't see typed text in input box. iPod5 - iOS7.0.2 - 使用链接中的小提琴:无法在输入框中看到键入的文本。 Pressing select redirects me to facebook.com (??? wtf ???) no idea what's going on there. 按选择将我重定向到facebook.com(??? wtf ???)不知道那里发生了什么。

UPDATE: (14-11-2013) 更新: (14-11-2013)

  • iOS 7.0.3 : Thanks to the comment from binki update that the .selectionStart and .selectionEnd does work. iOS版7.0.3:由于从注释binki更新的.selectionStart.selectionEnd 确实工作。

UPDATE: (15-01-2015) 更新: (15-01-2015)

  • iOS 8.xx : Thanks to the comment from Michael Siebert . iOS 8.xx:感谢Michael Siebert的评论。 Taken from the comment: I had to listen for both focus AND click events and then setTimeout/_.debounce to make it work in both cases: click the input or focus through tabbing 取自评论:我必须同时收听焦点和点击事件,然后设置时间输出/ _。去抖动以使其在两种情况下都有效:点击输入或通过标签对焦

Sorry, in my earlier post, I didn't notice the Javascript implying that you wanted an answer in Javascript. 对不起,在我之前的帖子中,我没有注意到Javascript意味着你想在Javascript中得到答案。

To get what you want in UIWebView with javascript , I have managed to scrape together two important pieces of info to get it to work. 为了在UIWebView中使用javascript获得您想要的内容,我已经设法将两个重要的信息拼凑在一起以使其工作。 Not sure about the mobile browser. 不确定移动浏览器。

  1. element.setSelectionRange(0,9999); does what we want 做我们想要的

  2. mouseUp event is undoing the selection mouseUp事件正在撤消选择

Thus (using Prototype): 因此(使用Prototype):

input.observe('focus', function() {
      this.setSelectionRange(0, 9999);
    });
    input.observe('mouseup', function(event) {
      event.stop();
    });

does the trick. 诀窍。

Matt 马特

It looks like focus will work but only when directly called from a native event. 看起来焦点会起作用,但只有在从本机事件直接调用时才会起作用。 calling focus using something like SetTimeout does not appear call up the keyboard. 使用类似SetTimeout之类的东西调用焦点不会出现调用键盘。 Control of the ios keyboard is very poor. 控制ios键盘非常差。 Its not a good situation. 这不是一个好的情况。

With iOS 7 on iPad the only way that I was able to make this work was to use actually <textarea></textarea> instead of <input> field. 使用iPad上的iOS 7,我能够完成这项工作的唯一方法是使用<textarea></textarea>而不是<input>字段。

eg 例如

<textarea onclick="this.setSelectionRange(0, 9999);">My text will be selected when textarea is clicked.</textarea>

How to prevent user from changing text inside area was more difficult, since is you make textarea readonly the selection trick won't work anymore. 如何防止用户更改区域内的文本更加困难,因为如果你只使用textarea,那么选择技巧将不再适用。

I went nuts looking for this solution, while all your responses did help it opened another can of worms for me. 我疯狂地寻找这个解决方案, 而你的所有回复确实帮助它为我打开了另一种蠕虫。

The client wanted the user to be able to click and select all , and also let the user 'tab' and select all on the iPad (with an external keyboard. I know, crazy...) 客户希望用户能够点击并选择所有让用户'标签'并在iPad上选择所有 (使用外部键盘。我知道,疯了......)

My solution to this problem was, rearrange the events. 我对这个问题的解决方案是重新排列事件。 First Focus , then Click , then touchstart . 首先关注 ,然后点击 ,然后触摸开始

$('#myFUBARid').on('focus click touchstart', function(e){
  $(this).get(0).setSelectionRange(0,9999);
  //$(this).css("color", "blue");
  e.preventDefault();
});

I hope this helps someone, as you lot have helped me countless times. 我希望这对某些人有所帮助,因为你们已经无数次帮助了我。

Something like the following is working for me for me on Webkit that comes with Android 2.2: 在Android 2.2附带的Webkit上,以下内容适用于我:

function trySelect(el) {
    setTimeout(function() {
        try {
            el.select();
        } catch (e) {
        }
    }, 0);
}

See Chromium Issue 32865 . 见Chromium Issue 32865

If you are using HTML5-compliant browsers, you can use placeholder="xxx" in your input tag. 如果您使用的是符合HTML5的浏览器,则可以在input标记中使用placeholder="xxx" That should do the job. 那应该做的。

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

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