在ajax事件上禁用/启用命令按钮

时间:2011-11-03 19:59:16

标签: ajax jsf jsf-2

我希望能够在命中按钮一旦命中后禁用它,并在事件侦听器运行并且呈现msg1时启用它。

<h:commandButton value="Submit">                    
  <f:ajax execute="@form" render="msg1" listener="{bean.method}" />
</h:commandButton>

我怎么能这样做?

更新:我发现我可以将onclick事件附加到commandButton元素本身来禁用它。如何检测侦听器方法已返回,以便再次启用该按钮?

2 个答案:

答案 0 :(得分:5)

你可以借助onevent <f:ajax>属性来帮助它,它指向一个处理JSF Ajax事件的JavaScript函数。

E.g:

<h:commandButton value="Submit">
  <f:ajax execute="@form" render="msg1" listener="#{bean.method}" onevent="handleDisableButton" />
</h:commandButton>

(请注意我在listener修复了错误的EL)

用这个JS:

function handleDisableButton(data) {
    var buttonElement = data.source; // The HTML DOM element which invoked the ajax event.
    var ajaxStatus = data.status; // Can be "begin", "complete" and "success".

    switch (ajaxStatus) {
        case "begin": // This is called right before ajax request is been sent.
            buttonElement.disabled = true;
            break;

        case "complete": // This is called right after ajax response is received.
            // We don't want to enable it yet here, right?
            break;

        case "success": // This is called when ajax response is successfully processed.
            buttonElement.disabled = false;
            break;
    }
}

答案 1 :(得分:0)

如果使用richfaces,则a4j:commandButton具有阻止此操作的status属性。