无法在"HTMLInputElement"上执行"setSelectionRange":输入元素的类型(“数字")不支持选择 [英] Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection

查看:618
本文介绍了无法在"HTMLInputElement"上执行"setSelectionRange":输入元素的类型(“数字")不支持选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击输入框时,我添加了以下代码以选择整个文本:

I added the code below to select whole text when user click on input box:

<input type="number" onclick="this.setSelectionRange(0, this.value.length)" name="quantity" />

但是我收到以下错误:

未捕获的InvalidStateError:无法在"HTMLInputElement"上执行"setSelectionRange":输入元素的类型(数字")不支持选择.

Uncaught InvalidStateError: Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection.

推荐答案

请改用input type="tel".

以您的示例为例:

<input type="tel" onclick="this.setSelectionRange(0, this.value.length)" name="quantity" />

这可以解决问题,并避免出现错误消息.

That will do the trick and avoid the error message.

在手机上,它会显示所需的数字键盘.

And on mobile phones, it shows up the desired number pad.

这篇关于无法在"HTMLInputElement"上执行"setSelectionRange":输入元素的类型(“数字")不支持选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆