输入字段中的文本无法删除
我有一个输入字段,如
是否有可能无法删除或选择static text
? 这是必需的,以便用户可以在“静态文本”之后输入文本。
我建议你这样做( 只是HTML和CSS解决方案 ):
把你的input[type=text]
放在一个label
并给出一些我在这里作为#txt
给出的id,并在css中做一些样式,如下所示:
#txt { border:none; outline:none; } label[for="txt"] { border:solid 1px #d5d5d5; padding: 0 0 0 5px; }
一个演示给你。
您可以使用包含在div
两个input
标记(其中一个禁用,作为“静态”文本),例如:
(您也可以使用readonly
属性而不是disabled
属性)
然后你可以设置标签的样式,使它们看起来像你想要的那样。
#wrapper { border: 1px solid black; display:inline-block } #static { width:30px; background: white; // not required if you color: black; // use readonly } input { border: none; outline: 0 none; }
DEMO
编辑:使用Javascript阻止用户选择静态文本:
document.getElementById('static').onselect = function (event) { event.target.selectionStart = event.target.selectionEnd; }
DEMO
心灵和Jai有点相同,我只是使用跨度而不是标签,只是个人的偏见。
Static text
然后将其设计为使其看起来像一个盒子:
input { display: block; margin: -1px 0 0 -1px; border: solid 1px #000; border-left: none; outline: none; height: 18px; } .static_text { padding: 0 5px; border: solid 1px #000; border-right: none; float: left; }
DEMO
您应该使用两个输入框,使一个禁用并保持其他可编辑并根据要求应用css。
您可以使用javascript的keydown事件来允许用户在文本框中键入其他文本,但阻止他们删除固定文本。 以下是一个例子。
您还可以使用jquery转换上述逻辑,这将更简单和可读。
的jsfiddle
#txt { border:none; outline:none; } label[for="txt"] { border:solid 1px #d5d5d5; padding: 0 0 0 5px; }