如何使用 jQuery 选择 JSF 组件?
我正在尝试使用 PrimeFaces 和 JSF 组件来实现 jQuery,但它无法正常工作。当我尝试对 HTML 标签执行相同操作时,它工作正常。
下面是带有 HTML 标签的代码,它可以与 jQuery 一起正常工作:
<input type="checkbox" id="check2"></input>
<h:outputText value="Check the box, if your permanent address is as same as current address."></h:outputText>
<h:message for="checkbox" style="color:red" />
with
$("#check2").change(function() {
if ($("#check2").is(":checked")) {
$("#p2").hide();
} else {
$("#p2").show();
}
});
这是带有 PrimeFaces/JSF 的代码,它不能与 jQuery 一起正常工作:
<p:selectManyCheckbox >
<f:selectItem itemLabel="1" value="one" id="rad" ></f:selectItem>
</p:selectManyCheckbox>
with
$("#rad").change(function() {
if ($("#rad:checked").val() == "one") {
$("#p2").hide();
} else {
$("#p2").show();
}
});
I am trying to implement jQuery with PrimeFaces and JSF components, but it's not working properly. When I tried to do the same with HTML tags it;s working properly.
Here is the code with HTML tags which works properly with jQuery:
<input type="checkbox" id="check2"></input>
<h:outputText value="Check the box, if your permanent address is as same as current address."></h:outputText>
<h:message for="checkbox" style="color:red" />
with
$("#check2").change(function() {
if ($("#check2").is(":checked")) {
$("#p2").hide();
} else {
$("#p2").show();
}
});
Here is the code with PrimeFaces/JSF which doesn't work properly with jQuery:
<p:selectManyCheckbox >
<f:selectItem itemLabel="1" value="one" id="rad" ></f:selectItem>
</p:selectManyCheckbox>
with
$("#rad").change(function() {
if ($("#rad:checked").val() == "one") {
$("#p2").hide();
} else {
$("#p2").show();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该意识到 jQuery 在客户端与 HTML DOM 树一起工作。 jQuery 并不像您在 JSF 源代码中编写的那样直接在 JSF 组件上工作,但 jQuery 直接在由这些 JSF 组件生成的 HTML DOM 树上工作。您需要在网络浏览器中打开页面并右键单击,然后查看源代码。您将看到 JSF 在生成的 HTML 输入元素的 ID 前面加上所有父元素
NamingContainer
组件(例如
,
等),以:
作为默认分隔符。例如,最终会生成 HTML,因为
您需要完全该 ID 选择元素。然而
:
是 CSS 标识符中表示伪选择器的特殊字符。要使用 jQuery 中的 CSS 选择器选择 ID 中带有:
的元素,您需要通过反斜杠对其进行转义,或者使用[id=...]
属性选择器或仅使用旧的getElementById()
:如果您在 ID 中看到自动生成的
j_idXXX
部分,其中XXX
表示增量数字,那么您必须给特定组件一个固定 ID,因为增量编号是动态的,并且会根据组件在树中的物理位置而发生变化。作为替代方案,您也可以只使用类名:
以 HTML as 结尾
,以便您可以将其获取为 as
这可以实现更好的抽象和可重用性。当然,这些元素并不是独一无二的。只有主要布局元素(如页眉、菜单、内容和页脚)确实是唯一的,但它们通常不在
NamingContainer
中。作为另一种选择,您可以将 HTML DOM 元素本身传递到函数中:
另请参阅:
You should realize that jQuery works with the HTML DOM tree in the client side. jQuery doesn't work directly on JSF components as you've written in the JSF source code, but jQuery works directly with the HTML DOM tree which is generated by those JSF components. You need to open the page in webbrowser and rightclick and then View Source. You'll see that JSF prepends the ID of the generated HTML input elements with the IDs of all parent
NamingContainer
components (such as<h:form>
,<h:dataTable>
, etc) with:
as default separator character. So for examplewill end up in generated HTML as
You need to select elements by exactly that ID instead. The
:
is however a special character in CSS identifiers representing a pseudo selector. To select an element with a:
in the ID using CSS selectors in jQuery, you need to either escape it by backslash or to use the[id=...]
attribute selector or just use the oldgetElementById()
:If you see an autogenerated
j_idXXX
part in the ID whereXXX
represents an incremental number, then you must give the particular component a fixed ID, because the incremental number is dynamic and is subject to changes depending on component's physical position in the tree.As an alternative, you can also just use a class name:
which ends up in HTML as
so that you can get it as
This allows for better abstraction and reusability. Surely those kind of elements are not unique. Only the main layout elements like header, menu, content and footer are really unique, but they are in turn usually not in a
NamingContainer
already.As again another alternative, you could just pass the HTML DOM element itself into the function:
See also:
您还可以使用 jQuery“属性包含选择器”(这里是 url http://api. jquery.com/attribute-contains-selector/)
例如,如果您有一个
并且您想对其对象执行某些操作,您可以使用它来选择它
,如果您想打印其值,您可以执行此
操作知道真实的元素的 html 标签您始终可以使用 firebug 或 ie 开发人员工具或查看源代码查看真正的 html 元素(在本例中微调器被翻译为输入)...
丹尼尔。
You also can use the jQuery "Attribute Contains Selector" (here is the url http://api.jquery.com/attribute-contains-selector/)
For example If you have a
and you want to do something on its object you can select it with
and if you want to print its value you can do this
In order to know the real html tag of the element you can always look at the real html element (in this case spinner was translated into input) using firebug or ie developer tools or view source...
Daniel.
如果您使用 RichFaces,您可以检查
rich:jQuery
组件。它允许您为 jQuery 组件指定服务器端 id。例如,您有具有指定服务器 id 的组件,那么您可以通过以下方式应用任何 jQuery 相关内容:有关更多信息,请检查 文档。
希望有帮助。
If you're using RichFaces you can check
rich:jQuery
comonent. It allows you to specify server side id for jQuery component. For example, you have component with specified server id, then you can apply any jQuery related stuff to in next way:For more info, please check doumentation.
Hope it helps.
看看,当我选择 experience=Yes 时,这会对您有所帮助,我的 id 为 dlg3 的对话框会弹出。如果值为 No,则不会打开
look this will help you when i select experience=Yes my dialoguebox which id is dlg3 is popup.and if value is No it will not open