将文本输入转换为数字输入

发布于 2024-12-20 11:33:39 字数 528 浏览 1 评论 0原文

目前我有一堆文本输入,我无法更改 HTML(因为它是预先生成的),但我想将它们从 type="text" 转换为 type="number",是否有一些我可以使用 jQuery 或 JavaScript 来实现这一目标吗?

这是一个针对移动设备的小型应用程序。

基本上我想转换

<input type="text" id="MyID" name="MyName">

<input type="number" id="MyID" name="MyName">

我正在尝试类似

<script>
  function DoWhenReady() {
    $('#MyID').attr('type', 'number');
  }
  $(document).ready(DoWhenReady);
</script>

在标题中的内容,但没有成功-建议?

Currently I have a bunch text inputs, which I can't change the HTML for (as it is pre-generated), but I want to convert them from type="text" to type="number", is there a bit of jQuery or JavaScript I can use to achieve this?

This is in a small application for mobile devices.

Basically I want to convert

<input type="text" id="MyID" name="MyName">

to

<input type="number" id="MyID" name="MyName">

I was trying something like

<script>
  function DoWhenReady() {
    $('#MyID').attr('type', 'number');
  }
  $(document).ready(DoWhenReady);
</script>

In the header, but without success - suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷荒 2024-12-27 11:33:39

尝试 $('#MyID').prop('type', 'number');属性)

Try $('#MyID').prop('type', 'number'); (jQuery 1.6+ required for prop)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文