< h:commandButton>的操作当“ disabled = true”时,不会被调用被设置 [英] Action of <h:commandButton> not getting invoked when "disabled=true" is set

查看:118
本文介绍了< h:commandButton>的操作当“ disabled = true”时,不会被调用被设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望选中T& C复选框时启用提交按钮。尽管我的书面逻辑工作正常,但是即使在选中复选框后启用h:commandButton的动作也不会被调用。最初,该按钮被禁用。如果我删除了disabled = TRUE属性,则该代码可以正常工作,但不能达到目的。这是我的代码:

I want the Submit button to be enabled when the T&C checkbox is checked. Though my written logic is working alright the action of h:commandButton is not getting invoked even after it is enabled after checking the checkbox. Initially the button is disabled. The code works fine if I remove the disabled="TRUE" attribute but it doesn't serve the purpose. Here is my code :

<script type="text/javascript">
$(document).ready(start);
function start() {

    $('.testing').click(
            function() {
                if ($(document.getElementById('form2:check'))
                        .is(':checked')) {
                    $(document.getElementById('form2:saveForm'))
                            .removeAttr('disabled');
                } else {
                    $(document.getElementById('form2:saveForm')).attr(
                            'disabled', 'disabled');
                }
            });
                 }
</script>

JSP代码:

<h:form id="form2">
    <div class="testing">
        <h:selectBooleanCheckbox id="check" />
                Agree           
            <li class="buttons ">
                <div class="center">
                    <h:commandButton id="saveForm" styleClass="btTxt submit"
                        type="submit" value="Submit"
                        action="#{declarationFormBean.formSubmit}" disabled="true"></h:commandButton>
                </div>
            </li>
</h:form>

请帮助。

推荐答案

这是因为您只是使用JavaScript / jQuery删除了JSF生成的HTML表示形式中的已禁用属性。这不会更改实际JSF组件中的 disabled 属性的任何内容,在解码动作事件时会参考该属性。

That's because you're solely removing the disabled attribute in the JSF-generated HTML representation using JavaScript/jQuery. This doesn't change anything to the disabled attribute in the actual JSF component which is consulted during decoding the action event.

这实际上是JSF的Goodthing TM ,因为否则,任何黑客都可以绕过任何禁用了JSF的 只读已渲染属性,只需在刚使用HTML DOM树时对其进行操作即可像这样 rendered =#{request.isUserInRole('ADMIN')} 来阻止它们。

This is actually a Good ThingTM of JSF, because otherwise any hacker would be able to bypass any JSF disabled, readonly and rendered attribute by just manipulating the HTML DOM tree while they are initially used to block them like so rendered="#{request.isUserInRole('ADMIN')}".

您需要通过JSF而不是JS / jQuery来启用按钮。

You need to enable the button by JSF instead of JS/jQuery.

<h:selectBooleanCheckbox binding="#{check}">
    <f:ajax render="saveForm" />
</h:selectBooleanCheckbox>
<h:commandButton id="saveForm" ... disabled="#{not check.value}" />

仅此而已。不需要自定义JS / jQuery混乱,也不需要任何其他JSF bean属性。上面的代码按原样完整。

That's all. No custom JS/jQuery mess necessary, nor any additional JSF bean properties. The above code is complete as-is.


  • < a href = https://stackoverflow.com/questions/4421839/what-is-the-need-of-jsf-when-ui-can-be-achieved-from-css-html-javascript-jquery/>当可以通过CSS,HTML,JavaScript,jQuery实现UI时,JSF有什么需要?

  • What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?

这篇关于&lt; h:commandButton&gt;的操作当“ disabled = true”时,不会被调用被设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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