使用 javascript/jquery 修复文本输入框的字符 [英] Fix a character to text input box using javascript/jquery
问题描述
我正在寻找一种将美元符号 $ '修复' 到文本输入框的方法,例如 <input type="text" value="$"/>
但让它不是可以删除默认值,以便无论用户输入什么,总会有一个 $ 符号前置"输入.
I am looking for a way to 'fix' a dollar symbol $ to a text input box eg <input type="text" value="$" />
but make it not possible for the default value to be removed, so that whatever the user inputs, there is always a $ symbol 'prepending' input.
干杯
推荐答案
background-image
是有可能的,但是很难维护,对字体大小的变化没有反应,使其成为 IMO 不太理想的选择.
There is the possibility of a background-image
but it's difficult to maintain, and doesn't react to font size changes, which makes it the less optimal choice IMO.
在我看来,更好的方法是:
A better way in my opionion would be:
在输入旁边放置一个
$
(实际上是在它之前).
Put a
<span>$</span>
next to the input (before it, actually).
给它position:relative;左:20px
.
然后 $ 符号移动到输入字段中.
The $ sign then moves into the input field.
然后,给输入字段padding-left: 24px
.
瞧!$ 符号位于输入字段中,不会遮挡任何内容,并且无法删除.
Voilá! The $ sign is in the input field, does not obscure anything, and cannot be removed.
这篇关于使用 javascript/jquery 修复文本输入框的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!