Primefaces,JavaScript和JSF不能很好地协同工作,或者我做错了什么 [英] Primefaces, JavaScript, and JSF does not work well together or am I doing something wrong

查看:69
本文介绍了Primefaces,JavaScript和JSF不能很好地协同工作,或者我做错了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很简单

<p:commandLink value="Tom" onclick="document.getElementById('tom').focus()"/><br/>
<input id="tom"/>

当您单击汤姆"时,文本框将获得焦点.太好了,现在试试这个

When u click on the Tom, the textbox get focus. Great, now try this

<p:commandLink value="Tom" onclick="document.getElementById('tom').focus()"/><br/>
<h:inputText id="tom"/> <br/>

当我单击没有任何反应时,我检查了萤火虫,看到了

when I click nothing happen, I check firebug, I see

document.getElementById("tom") is null

当我尝试使用jQuery $('#tom').focus()时,什么也没有发生,没有错误,但是也没有得到关注.当我从萤火虫看到时,这是response(不确定这是否是服务器的响应)

When I try to use jQuery $('#tom').focus(), nothing happen, no error, but did not get focus either. This is the response (not sure if this is the response from the server) when I see from firebug

<?xml version="1.0" encoding="utf-8"?>
<partial-response>
    <changes>
       <update id="javax.faces.ViewState"><![CDATA[455334589763307998:-2971181471269134244]]></update>
    </changes>
    <extension primefacesCallbackParam="validationFailed">{"validationFailed":false}</extension>
</partial-response>


推荐答案

在JSF中,元素ID的前缀是包含元素的表单的ID(更常见的是,其ID的前缀是所有父元素的ID实现 NamingContainer 接口的组件) .例如:

In JSF, the ID of elements are prefixed by the ID of the form that contains them (more generally, their ID are prefixed by the ID of all the parent components that implements the NamingContainer interface). For example:

<h:form id="myForm">
    <h:inputText id="tom" .../>

将生成以下HTML代码:

will generate the following HTML code:

<input id="myForm:tom" ...>

要访问<input>,您必须使用myForm:tom ID,而不是tom ID本身.

To access the <input> you must use the myForm:tom ID and not the tom ID itself.

使用jQuery,您将不得不使用$("myForm\:tom").focus();

With jQuery, you will have to use $("myForm\:tom").focus();

这篇关于Primefaces,JavaScript和JSF不能很好地协同工作,或者我做错了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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