【问题标题】:color change of <p:commandButton> after every onclick每次点击后 <p:commandButton> 的颜色变化
【发布时间】:2015-09-25 09:02:06
【问题描述】:

我在 jsf 页面中的代码如下所示,下面的代码每次点击都会弹出窗口,但同时我想为每次点击更改颜色 p:commandButton。

<h:form id="form">

 <p:commandButton id="button01" oncomplete="PF('test01').show()" value="sysno_01" icon="button" update=":form01:dialog01" />
</h:form>

  <h:form id="form01">
         <p:dialog header="Login" widgetVar="test01" id="dialog01"
            resizable="true" showEffect="explode" hideEffect="explode">

            <p:panelGrid id="display01" columns="2">
            <h:outputText value=" First name" />
            <h:inputText id="fnme01" value="#{studentLoginBean.studentname}" />
            <h:outputText value=" last name" />
            <h:inputText id="lnme01" value="#{studentLoginBean.lastname}" />
            </p:panelGrid>
            <h:commandButton actionListener="#{studentLoginBean.login}" value="Login">
             <f:param name="action" value="01" />
            </h:commandButton>  
            <h:commandButton actionListener="#{studentLoginBean.logout}" value="Logout">
                <f:param name="action" value="01" />
            </h:commandButton>
        </p:dialog> 
</h:form>

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    试试这个:为每个按钮输入class="changeColor",如下所示

    <h:commandButton actionListener="#{studentLoginBean.login}" value="Login" class="changeColor">
             <f:param name="action" value="01" />
    </h:commandButton>  
    <h:commandButton actionListener="#{studentLoginBean.logout}" value="Logout" class="changeColor">
                <f:param name="action" value="01" />
    </h:commandButton>
    

    然后在其上注册一个 jquery 点击处理程序并更改其颜色

    <script>
        $(function(){
             var colors = ['red','green','blue','pink','yellow','gray','white'];
             var len = colors.length;
             var counter = 0;
             $('.changeColor').click(function(){
                 $(this).css('background-color', colors[counter]);
                 counter++;
                 //reset counter
                 if(counter==len)
                   counter = 0;
             });
        });
    </scrip>
    

    【讨论】:

    • 将整个代码(带有&lt;script&gt;标签的代码)放在页面的&lt;head&gt;标签下或&lt;body&gt;标签内也可以。
    • 我想在点击弹出窗口后改变 p:commandButton 的颜色
    • 因此您希望在每次点击&lt;p:dialog 时更改按钮颜色。您的意思是当用户单击打开的弹出窗口上的任意位置时更改颜色?
    • &lt;p:commandButton id="button01" oncomplete="PF('test01').show()" 这个按钮点击打开一个弹出窗口,当弹出窗口显示你想改变按钮的颜色?您希望弹出窗口内的两个按钮颜色相同或不同?
    • 那么上面的代码只需将class="changeColor" 添加到该按钮并将其从弹出按钮中删除
    猜你喜欢
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多