简体   繁体   English

如何将占位符放在struts文本字段标记中?

[英]How can I put a placeholder in a struts textfield tag?

i got error because am using placeholder attribute in struts tags.... 我得到错误,因为我在struts标签中使用占位符属性....

<html:text property="name" styleClass="form-control" placeholder="some text"/>

how can resolve the problem,pls help me. 如何解决问题,请帮帮我。

Thanks in Advance. 提前致谢。

Use jQuery attr like below: 使用jQuery attr如下:

<html:text property="name" styleClass="form-control" styleId="abc" />

JavaScript code: JavaScript代码:

$(function() {
    $("#abc").attr("placeholder", "some text");
});

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 trip in the request. 该属性的值name必须与形式的属性相匹配的要求跳闸

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 如果您注意到,占位符属性可以使用完整表单呈现

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);
    });
});

or this answer could help you. 或者这个答案可以帮助你。

使用javascript事件的简单替代方法如下:

<html:text property="username" value="Username" onclick="this.value=''" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM