如何在网页上为输入框添加固定的占位文本并防止用户删除它?

huangapple go评论58阅读模式
英文:

How to add fixed placeholder text to an input box and prevent the user from deleting it on a web page?

问题

I understand that you want a translation of the provided text, excluding the code. Here's the translation:

制作一个新产品的订购表,其中一个列是指定产品的RAL颜色。

我希望有一个固定的文本,用户无法删除,所以他们只需要在之后输入一个与RAL颜色相关的四位数字。

我遇到的问题是输入超过了固定文本而不是在其后 - 请参见下面的截图。

请忽略表格的设计,因为我目前正在处理功能,它看起来有点糟糕。

HTML:

<td class="lst-input-ral-box">
    <input value="" id="ral" maxlength="4" autofocus="autofocus">
    <span class="ral-placeholder">RAL</span>
</td>

CSS:

.lst-input-ral-box {
    position: relative;
}
.input {
    display: block;
    border: 1px solid #d7d6d6;
    background: #fff;
    padding: 10px 10px 10px 20px;
    width: 195px;
}
.ral-placeholder {
    position: absolute;
    display: block;
    left: 5px;
    top: 3px;
    z-index: 9;
}

希望这有所帮助,如果您有任何问题,请随时提出。

英文:

Working on an ordering table for a new product and one of the columns is to specify the RAL color of the product.

I want to have fixed text that the user can't delete so all they have to do is enter a four digit number after that which relates to the RAL colour.

The problem I'm having is the input is going over the fixed text and not after it - see below.

如何在网页上为输入框添加固定的占位文本并防止用户删除它?

Please ignore the design of the table as I'm working on the functionaility for now and its looking a bit rubbish.

HTML:

&lt;td class=&quot;lst-input-ral-box&quot;&gt;
     &lt;input value=&quot;&quot; id=&quot;ral&quot; maxlength=&quot;4&quot; autofocus=&quot;autofocus&quot;&gt;
     &lt;span class=&quot;ral-placeholder&quot;&gt;RAL&lt;/span&gt;
 &lt;/td&gt;

CSS:

.lst-input-ral-box {
   position: relative; 
}
.input {
    display: block; 
    border: 1px solid #d7d6d6; 
    background: #fff;
    padding: 10px 10px 10px 20px;
    width: 195px;
}
.ral-placeholder {
    position: absolute; 
    display: block;
    left: 5px; 
    top: 3px; 
    z-index: 9;  
}

Any help on this would be greatly appreciated as I'm fairly new to 'complex' coding.

答案1

得分: 4

不要将静态文本放在输入框内部,而是放在输入框之前。您可以通过从输入框中移除边框/轮廓等,并将其添加到包含元素中来样式化它,使其“看起来”像是在输入框内部。例如:

<!-- language: lang-css -->
.lst-input-ral-box {
  border: 1px solid #d7d6d6;
}
.lst-input-ral-box:focus-within {
  border: 1px solid #000;
}
input {
  border: none;
  outline: none;
}

<!-- language: lang-html -->
<table>
  <tr>
    <td class="lst-input-ral-box">
      <span class="ral-placeholder">RAL</span>
      <input value="" id="ral" maxlength="4" autofocus="autofocus">
    </td>
  </tr>
</table>

编辑: 为了完整起见,可以通过将 &lt;span&gt; 更改为 &lt;label&gt; 进一步改进,这样激活时将聚焦到 &lt;input&gt;。我还减小了两个包含元素之间的“间隙”,所以完全可点击的区域应该会激活 &lt;input&gt;

<!-- language: lang-css -->
.lst-input-ral-box {
  border: 1px solid #d7d6d6;
  display: flex;
  gap: 0;
}
.lst-input-ral-box:focus-within {
  border: 1px solid #000;
}
input {
  border: none;
  outline: none;
}

<!-- language: lang-html -->
<table>
  <tr>
    <td class="lst-input-ral-box">
      <label for="ral" class="ral-placeholder">RAL</label>
      <input value="" id="ral" maxlength="4" autofocus="autofocus">
    </td>
  </tr>
</table>

请注意,&lt;input&gt;&lt;label&gt; 具有重要的语义含义。在这里,应考虑可访问性,因为并非所有用户都会视觉上观察或关心这种样式上的放置。

这个元素是否为输入框添加了“标签”?从语义上来说,这由您决定。还可以使用 aria- 属性的组合和其他功能来实现合理的设计,同时保持标记的语义清晰。

英文:

Don't put the static text inside the input, put it before the input. You can style it to "appear" to be inside the input by removing the border/outline/etc. from the input and adding it to the containing element. For example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.lst-input-ral-box {
  border: 1px solid #d7d6d6;
}
.lst-input-ral-box:focus-within {
  border: 1px solid #000;
}
input {
  border: none;
  outline: none;
}

<!-- language: lang-html -->

&lt;table&gt;
  &lt;tr&gt;
    &lt;td class=&quot;lst-input-ral-box&quot;&gt;
     &lt;span class=&quot;ral-placeholder&quot;&gt;RAL&lt;/span&gt;
     &lt;input value=&quot;&quot; id=&quot;ral&quot; maxlength=&quot;4&quot; autofocus=&quot;autofocus&quot;&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

Edit: For completeness, this can be further improved by changing the &lt;span&gt; to a &lt;label&gt;, which will focus the &lt;input&gt; when activated. I've also reduced the "gap" between the two contained elements, so the full clickable area should activate the &lt;input&gt;:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.lst-input-ral-box {
  border: 1px solid #d7d6d6;
  display: flex;
  gap: 0;
}
.lst-input-ral-box:focus-within {
  border: 1px solid #000;
}
input {
  border: none;
  outline: none;
}

<!-- language: lang-html -->

&lt;table&gt;
  &lt;tr&gt;
    &lt;td class=&quot;lst-input-ral-box&quot;&gt;
     &lt;label for=&quot;ral&quot; class=&quot;ral-placeholder&quot;&gt;RAL&lt;/label&gt;
     &lt;input value=&quot;&quot; id=&quot;ral&quot; maxlength=&quot;4&quot; autofocus=&quot;autofocus&quot;&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

Note that a &lt;label&gt; for an &lt;input&gt; has an important semantic meaning. One should take accessibility into consideration here, as not all users will visually observe or care about this stylistic placement.

Does this element "label" the input? Semantically that's for you to decide. Combinations of aria- attributes and other functionality can also be used to achieve reasonable designs while keeping the markup semantically clear.

答案2

得分: 2

I have well understood, you want to add 4 digits after the "RAL" constant right?

If so, you should replace:

<input value="" id="ral" maxlength="4" autofocus="autofocus">
<span class="ral-placeholder">RAL</span>

with:

<label for="ral" class="ral-placeholder">RAL</label>
<input type="text" value="" id="ral" name="ral" maxlength="4" autofocus="autofocus">

PS: you have to know that the name attribute is a good practice to use correctly in the for="" from <label> tag, even if id is working also.

英文:

if I have well understood, you want to add 4 digits after the "RAL" constant right ?

Is so, you should replace

&lt;input value=&quot;&quot; id=&quot;ral&quot; maxlength=&quot;4&quot; autofocus=&quot;autofocus&quot;&gt;
&lt;span class=&quot;ral-placeholder&quot;&gt;RAL&lt;/span&gt;

with:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;label for=&quot;ral&quot; class=&quot;ral-placeholder&quot;&gt;RAL&lt;/label&gt;
&lt;input type=&quot;text&quot; value=&quot;&quot; id=&quot;ral&quot; name=&quot;ral&quot; maxlength=&quot;4&quot; autofocus=&quot;autofocus&quot;&gt;

<!-- end snippet -->

PS : you have to know that the name attribute is a good practice to use correctly in the for=&quot;&quot; from &lt;label&gt; tag, even if id is working also.

huangapple
  • 本文由 发表于 2023年5月24日 19:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定