简体   繁体   English

如何在HTML输入文本中添加默认文本

[英]How can I add a default text in my HTML input text

How can I add a default text in my HTML text just like in the image below 像下面的图片一样,如何在HTML文本中添加默认文本

在此处输入图片说明

Right now this is my html codes: 现在,这是我的html代码:

<input type="text" class="form-control input-sm" name="guardian_officeno" placeholder="Office #" required pattern="[0-9].\d{8}|\d{11}" title="Only Numbers are accepted and must be 10 or 11 numbers"></td>
  </div>

EDIT: 编辑:

How can I add text that cannot be erased 如何添加无法删除的文本

As per the recent update of your question, you need to add readonly attribute to your input tag. 根据您问题的最新更新,您需要在input标签中添加readonly属性。 If you are trying to disable part input, which is not possible, refer to last section of my answer. 如果您试图禁用部分输入,那是不可能的,请参考我的答案的最后一部分。

<input type="text" value="SOME_DEFAULT_VALUE" readonly>

Adding readonly will prevent users to change the text in your input box. 添加readonly将阻止用户更改input框中的文本。

Note: Don't rely on readonly as it can be easily overridden using dev tools. 注意:不要依赖readonly ,因为可以使用开发工具轻松地将其覆盖。 Make sure you do have server side checks for the same. 确保确实有服务器端检查。


Before you updated your question 在您更新问题之前

Add value attribute to your input tag if you want some default text in the input box 添加value属性您input的标签,如果你想在一些默认的文本input

<input type="text" value="Default Val">

If you are talking about the prefix ie country specific STD code like +63, then you can do is split up your input tags, and set a default value in the first tag and let your user write the number in the other tag. 如果您在谈论前缀,即国家特定的STD代码,例如+63,则可以拆分input标签,并在第一个标签中设置默认值,然后让用户在另一个标签中输入数字。

Edit, with the tag value: 使用标签值进行编辑:

https://www.w3schools.com/tags/att_input_value.asp https://www.w3schools.com/tags/att_input_value.asp

<input type="text" class="form-control input-sm" name="guardian_officeno" placeholder="Office #" required pattern="[0-9].\d{8}|\d{11}" title="Only Numbers are accepted and must be 10 or 11 numbers"  value="default text"></td>
  </div>

you mean you want to add a default text in "input" tag? 您的意思是您想在“输入”标签中添加默认文本? you need to add "value" attribute in there! 您需要在其中添加“值”属性!

<input type="text" class="form-control input-sm" name="guardian_officeno" placeholder="Office #" required pattern="[0-9].\d{8}|\d{11}" title="Only Numbers are accepted and must be 10 or 11 numbers" value="put your default text right here"></td>

input tag not support for before attribute in css, but you can using wrapper by other tags. 输入标签不支持CSS中的before属性,但是您可以使用其他标签的包装器。 look for example below: 在下面查找示例:

html + css HTML + CSS

<div class="input-row">
    Phone number:<span id="phoneNumber"><input type="text" name="phoneNumber" value="" placeholder="1234567890"></span>
</div>
<style>
    .input-row {
        display: inline-block;
        line-height: 1;
    }
    span#phoneNumber::before {
        content: '+63';
        display: inline-block;
        border: 1px solid #ccc;
        padding: 1px;
        line-height: 20px;
        margin: 0px;
    }
    input[name='phoneNumber']{
        line-height: 18px;
        border: 1px solid #ccc;
        margin: 0px;
        padding: 2px;
    }
</style>

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

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