使用ajax启用/禁用按钮时未触发操作 [英] Action not triggered when button is enabled/disabled with ajax

查看:235
本文介绍了使用ajax启用/禁用按钮时未触发操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入一个对我没有任何意义的问题.我有一个列表框,它使用简单的Ajax触发selectionChange-Events.这个想法是直到选择列表中的一个项目,编辑按钮才被启用.因此,我创建了以下代码.

<h:form>
    <h:selectManyListbox value="#{bean.selectedIds}">
        <f:selectItems value="#{bean.listOfItems}" />
        <f:ajax render="edit"
            listener="#{bean.selectionChanged}" />
    </h:selectManyListbox>
    <br />

    <h:commandButton id="add" value="#{msgs.add}"
        action="#{bean.addNew}" />

    <h:commandButton id="edit" value="#{msgs.edit}"
        disabled="#{bean.editButtonDisabled}"
        action="#{bean.edit}" />
</h:form>

该按钮可以根据需要启用和禁用,但事实证明,edit-button不会触发任何操作(我在Bean中的add-and edit-method中添加了一些sysout,并且从未调用过edit-method )...而不是html更改.上面的代码嵌套在一个简单的div中.当我单击编辑"时,整个表单在该div之外.

当我将此ajax行为添加到添加按钮时,也会发生同样的情况,反之亦然,当我从编辑按钮中删除残疾人属性时,一切正常?

我已经看过BalusC回答此处,但我找不到与该列表有关的任何错误.没有嵌套的表单等等……它只是一个带有模板的简单页面.

我在JBoss 7.1.Final上使用Mojarra 2.1.2.

感谢您的帮助.谢谢

解决方案

此问题已由您链接的答案的第5点解决.

在表单提交请求的应用请求值阶段,组件和所有父组件的rendered属性不应求值为false. JSF将随后对其进行重新检查,以作为防范被篡改/被入侵请求的保护措施的一部分.将Bean放入视图范围和/或确保在Bean的(post)构造函数中预先初始化条件应将其修复. 这同样适用于组件的disabled属性,在处理表单提交过程中不应将其评估为true.

我建议将BikeManagementPanelBean上的@RequestScoped更改为@ViewScoped.

I am stuck with a problem that doesnt make any sense to me. I have a listbox which fires selectionChange-Events with simple Ajax. The idea is that the edit-button isnt enabled until a item in the list is selected. So I created the following code.

<h:form>
    <h:selectManyListbox value="#{bean.selectedIds}">
        <f:selectItems value="#{bean.listOfItems}" />
        <f:ajax render="edit"
            listener="#{bean.selectionChanged}" />
    </h:selectManyListbox>
    <br />

    <h:commandButton id="add" value="#{msgs.add}"
        action="#{bean.addNew}" />

    <h:commandButton id="edit" value="#{msgs.edit}"
        disabled="#{bean.editButtonDisabled}"
        action="#{bean.edit}" />
</h:form>

The button is enabled and disabled as I wish but as it turns out, the edit-button isnt triggering any action (I added some sysout to the add- and edit-method in the bean and the edit-method is never called)...instead the html changes. The above code is nested in a simple div. When I click edit, the whole form is outside of that div.

When I add this ajax-behavior to the add-button, the same happens there and vice versa, when I remove the disabled-attribute from the edit-button everything works???

I already had a look at BalusC answer here but I couldnt find any mistake related to that list. No nested forms and so on...its just a simple page with a template.

I am using Mojarra 2.1.2 on JBoss 7.1.Final.

Any help is appreciated. Thanks

解决方案

This issue is covered by point 5 of the answer which you've linked.

The rendered attribute of the component and all of the parent components should not evaluate to false during the apply request values phase of the form submit request. JSF will namely recheck it then as part of safeguard against tampered/hacked requests. Putting the bean in the view scope and/or making sure that you're preinitializing the condition in (post)constructor of the bean should fix it. The same applies to the disabled attribute of the component, which should not evaluate to true during processing the form submit.

I suggest to change @RequestScoped on BikeManagementPanelBean to @ViewScoped.

这篇关于使用ajax启用/禁用按钮时未触发操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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