0

我在一个 ASPX 页面中有一个简单的表单,它有很多<label>配对<asp:TextBox>来构建表单的开销。

我需要在文本框后面添加一个字符串以指示该字段是强制性的。我尝试在字段后添加 a <span>、 a<em>或 a <div>,但它仍会在文本框底部显示消息。

我有什么办法可以做到这一点?

编辑: 我的意思是文本框的右侧,而不是水印中的后面。我的错。

编辑示例代码:
我已经尝试了所有建议,但仍然无法正常工作,考虑是否是我的代码问题。以下是我的代码:

<label>Telephone No.</label>
<asp:TextBox ID="txtTelNo" runat="server"></asp:TextBox>
<span class="afterInput">test</span> 

任何指针?谢谢。

编辑答案
事实证明,问题在于 css 属性。我使用的模板具有分配给display: block属性的所有输入,这使得<input>元素之后的任何内容都被按下。

在创建自定义 css 类display: inline-block并适当地分配给它们之后,我设法得到了我想要的结果。

非常感谢您提供的答案,尤其是:after属性和水印属性。

4

4 回答 4

2

http://jsfiddle.net/ekWG9/

.required:after{
    content: "*";
    color: red;   
}​

<label>A box</label><input type="text" value="Hello" /><span class="required"></span>​

<!-- alternative HTML -->

<span class="required"><label>A box</label><input type="text" value="Hello" /></span>​

使用:after伪元素选择器可以让您从标记中取出文字内容(例如,您不必一遍又一遍地重复“*”)。

您还可以使用相对或绝对定位来调整:after伪元素内容的位置。示例:http: //jsfiddle.net/ekWG9/1/

于 2012-09-06T05:26:28.957 回答
1

By behind the textbox, seems you are talking of watermark text.

You could use the TextBoxWatermark from ajaxcontrol toolkit.

There are also several jQuery alternatives to implement it.

html5 also has browser support for watermarks:

<input name="q" placeholder="Go to a Website">

You could add an attribute to your control to that effect.

于 2012-09-06T05:20:51.387 回答
0

Use css for this purpose:

span.clsRequired {
    float:left;
    margin:2px 0 0 3px;
    color:red;

}

And your span before text box looks like:

<span class="clsRequired ">*</span>
于 2012-09-06T05:23:36.720 回答
0
you should try something like this

 <body>
  <form id="form1">
  <asp:TextBox ID="TextBox1" runat="server"/>&nbsp;<span>Required field</span>
 </form>
 </body>
于 2012-09-06T05:35:23.990 回答