1

我想用 jquery 获得一个文本框对象。

$(function() {
    // create a datepicker with default settings
    $("[id='#{fbForm.beginDate}']").mobiscroll().date({
        invalid: {
            daysOfWeek: [0, 6], 
            daysOfMonth: ['5/1', '12/24', '12/25']
        }, 
        theme: 'android-ics light', 
        display: 'bottom', 
        mode: 'scroller', 
        dateOrder: 'mmD ddyy'
    });
});

这不行吗?为什么是什么问题

4

2 回答 2

1

假设你有

<h:form id="myForm">
    <h:inputText id="myText" />
</h:form>

您可以使用 jQuery 访问组件

var myText = $('#myForm\\:myText');

例子:

<script type="text/javascript">
    //<![CDATA[
    window.onload = function() {
        var myTextValue = $('#myForm\\:myText').val();
        alert(myTextValue);
    };
    //]]>
</script>

<h:form id="myForm">
    <h:inputText id="myText" value="#{fbForm.beginDate}" />
</h:form>

或者您可以通过添加来使用普通组件prependId="false"ID <h:form>

<script type="text/javascript">
    //<![CDATA[
    window.onload = function() {
        var myTextValue = $('#myText').val();
        alert(myTextValue);
    };
    //]]>
</script>

<h:form prependId="false">
    <h:inputText id="myText" value="#{fbForm.beginDate}" />
</h:form>
于 2013-03-13T02:31:11.287 回答
0

万一您使用的是 RichFaces,您也​​可以使用:

rich:clientId(id) - 通过它的短 id 返回组件的客户端 ID。

rich:element(id) - 返回传递的短 id 的 DOM 元素

rich:component(id) - 返回 Richfaces 客户端组件实例以调用一些 API 方法。

rich:isUserInRole(role) - 如果用户具有指定角色则返回。

rich:findComponent(id) - 返回给定短 id 的组件实例

rich:jQuery(id) - 返回一个带有给定短 id 元素的 jQuery 对象

例子:

<h:form>
    <h:inputText id="input">
        <a4j:ajax render="out" />
    </h:inputText>
    <h:outputText value="#{rich:findComponent('input').value}" id="out" />
</h:form>

有关如何执行此操作的更多信息和示例:

http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=functions&skin=blueSky

问候,

于 2013-03-13T09:34:12.240 回答