InputText组件中的默认文本(占位符) [英] Default text (placeholder) in InputText component

查看:347
本文介绍了InputText组件中的默认文本(占位符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JSF/Primefaces inputText 组件中显示默认的信息性文本.

I want to display a default Informative text in JSF/Primefaces inputText component.

  • 当用户单击文本"字段时,该文本应消失.
  • 在他不键入任何内容的情况下单击外部时出现.
  • 如果用户提交的表单没有任何值,则不应将此默认值设置为Bean的属性.

我知道在ManagedBean中将默认值设置为BeanProperty可以使用,但我不希望这样做.
如果可能,请提出任何JQuery调整建议.
将我重定向到正确的问题,如果该问题重复.

I'm aware that setting the default value to BeanProperty in ManagedBean will work,but I don't want that.
Please suggest any JQuery tweaks,If possible.
Redirect me to right Question if this question is Duplicate.

推荐答案

Primefaces 提供了最新版本的placeholder属性,您可以将其用于p:inputText.此外,还有一个 p:watermark 组件,该组件可创建基于JS的解决方案与旧版浏览器兼容.因此,您不一定需要在支持bean中设置默认值.只需使用以下解决方案之一:

Primefaces provides a placeholder attribute in its latest versions which you can use for p:inputText. Also, there is a p:watermark component which creates a JS based solution for legacy browser compatibility. So you don't definitely need to set a default value into the backing bean. Just use one of the following solutions:

<h:outputLabel value="Search: "/>  
<p:inputText id="search_input_id" value="#{watermarkBean.keyword}" 
    required="true" label="Keyword" placeholder="search" />  

对于旧版浏览器:

<h:outputLabel value="Search: "/>  
<p:inputText id="search_input_id" value="#{watermarkBean.keyword}" 
    required="true" label="Keyword" />  
<p:watermark for="search_input_id" value="search" />

此外,如果使用 JSF 2.2 ,则可以使用其直通属性.将xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"命名空间添加到页面中,您可以通过以下方式实现 JSF h:inputText Primefaces p:inputText:

Also if using JSF 2.2, you can use its passthrough attributes. Adding xmlns:pt="http://xmlns.jcp.org/jsf/passthrough" namespace to your page, you can achieve in the following way, both for JSF h:inputText and Primefaces p:inputText:

<h:inputText value="#{watermarkBean.keyword}"
    pt:placeholder="search" />

或使用TagHandler将其包装到标签中:

Or wrapping it into the tag with a TagHandler:

<h:inputText value="#{watermarkBean.keyword}">
    <f:passThroughAttribute name="placeholder"
        value="search"/>
</h:inputText>

哪个创建基于HTML 5的输入,并带有placeholder属性:

Which creates HTML 5 based input with placeholder attribute:

<input placeholder="search"> 

这篇关于InputText组件中的默认文本(占位符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆