Java 如何在 struts 文本字段标记中放置占位符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20209487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I put a placeholder in a struts textfield tag?
提问by Kannan Arumugam
i got error because am using placeholder attribute in struts tags....
我收到错误,因为我在 struts 标签中使用了占位符属性....
<html:text property="name" styleClass="form-control" placeholder="some text"/>
<html:text property="name" styleClass="form-control" placeholder="some text"/>
how can resolve the problem,pls help me.
如何解决问题,请帮助我。
Thanks in Advance.
提前致谢。
采纳答案by rajesh kakawat
Use jQuery attr
like below:
attr
像下面这样使用jQuery :
<html:text property="name" styleClass="form-control" styleId="abc" />
JavaScript code:
JavaScript 代码:
$(function() {
$("#abc").attr("placeholder", "some text");
});
回答by Saif Asif
There is a placeholder attribute in struts tags as well called placeholder
struts 标签中有一个占位符属性也称为占位符
<s:form action="Welcome">
<s:textfield name="username" label="Username" placeholder="Enter Your Name" />
<s:password name="password" label="Password" placeholder="Password"/>
<s:submit/>
</s:form>
Edit
编辑
There isn't an attribute with the name placeholder (sorry for the confusion), but if you type in placeholder like my code sample, the struts form will be evaluated as below
没有名称占位符的属性(抱歉造成混淆),但是如果您像我的代码示例一样输入占位符,那么 struts 表单将被评估如下
<form id="Welcome" name="Welcome" action="/User/Welcome.action" method="post">
<input type="text" name="username" value="" id="Welcome_username" placeholder="Enter your Name">
<input type="password" name="password" id="Welcome_password" placeholder="Password">
</form>
If you notice, the placeholder attribute renders up just fine with the complete form
如果您注意到,占位符属性可以很好地呈现完整的表单
回答by Jai
You can try with jQuery to add this attribute when document gets ready:
当文档准备好时,您可以尝试使用 jQuery 添加此属性:
<html:text property="name" styleClass="form-control" styleId="xyz" />
then try adding this jquery:
然后尝试添加这个 jquery:
$(function(){
$(':input').each(function(){
$(this).attr("placeholder", this.id);
});
});
回答by Paul Vargas
Just replace:
只需更换:
<html:text property="name" styleClass="form-control" placeholder="some text" />
With:
和:
<input type="text" name="property" class="form-control" placeholder="some text"
│
└─── Form property ────┐
│
value="<bean:write name="name" property="property" />" />
│
Name of form-bean ─┘
The value of the attribute name
must match the property of your form to tripin the request.
该属性的值name
必须对您的窗体的属性相匹配的行程在请求中。
回答by NEERAJ TANDON
A simple alternative using javascript event is as follows :
使用 javascript 事件的一个简单替代方法如下:
<html:text property="username" value="Username" onclick="this.value=''" />