HTML输入中不可更改的前导字符? [英] unchangeable leading characters in HTML input?

查看:83
本文介绍了HTML输入中不可更改的前导字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成一个输入元素,该输入元素的默认起始值​​不可更改?例如,我正在从用户那里寻找一个数字,但是我希望"333"已经存在于输入文本框中,因为所有输入都将从该数字开始.我也不希望用户能够更改它.我还需要将333用作值的一部分,而不仅仅是通过样式添加,因为我需要对其进行验证.

How do I generate an input element that has a default starting value in there that is unchangeable? For example, I am looking for a number from the user but I want '333' to be already in the input text box since all inputs will start with that number. I also don't want the user to be able to change it. I need the 333 to be part of the value also rather than just being added via style since I need to do validation on it.

推荐答案

使用JavaScript的一种可能性:

One possibilty using JavaScript:

nine = document.getElementById("nine");
nine.addEventListener("keydown", function (ev) {
  var el = this;
  var value = el.value;
  setTimeout(function () {
    if (el.value.indexOf("333") != 0) {
      el.value = value;
    } 
  }, 0);
});

<input type="text" value="333" id="nine" />

这篇关于HTML输入中不可更改的前导字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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