您可以使用该onclick
属性在调用操作之前执行一些 JavaScript 代码。
例如:
<h:commandButton ... onclick="document.body.style.background='pink'">
然而,最好为此使用onevent
属性<f:ajax>
,以便您可以恢复更改:
<h:commandButton ...>
<f:ajax ... onevent="handleDo" />
</h:commandButton>
<img id="progress" src="progress.gif" class="hidden" />
例如,它禁用/启用按钮并显示/隐藏进度图像:
function handleDo(data) {
var status = data.status; // Can be 'begin', 'complete' and 'success'.
var button = data.source; // The HTML element which triggered the ajax.
var image = document.getElementById("progress"); // Ajax loading gif?
switch (status) {
case 'begin': // This is called right before ajax request is been sent.
button.disabled = true;
image.style.display = "block";
break;
case 'complete': // This is called right after ajax response is received.
image.style.display = "none";
break;
case 'success': // This is called when ajax response is successfully processed.
button.disabled = false;
break;
}
}