How can I put a placeholder in a struts textfield tag?

*爱你&永不变心* 提交于 2019-12-22 02:53:12

问题


i got error because am using placeholder attribute in struts tags....

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

how can resolve the problem,pls help me.

Thanks in Advance.


回答1:


Use jQuery attr like below:

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

JavaScript code:

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



回答2:


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.




回答3:


There is a placeholder attribute in struts tags as well called placeholder

<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

<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




回答4:


You can try with jQuery to add this attribute when document gets ready:

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

then try adding this jquery:

$(function(){
    $(':input').each(function(){
       $(this).attr("placeholder", this.id);
    });
});

or this answer could help you.




回答5:


A simple alternative using javascript event is as follows :

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


来源:https://stackoverflow.com/questions/20209487/how-can-i-put-a-placeholder-in-a-struts-textfield-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!