简体   繁体   中英

a4j:commandLink with onClick return true or false not properly working

My code is like below,

<a4j:commandLink  oncomplete="openSignatureModel();" id="requestText"  reRender="sigPassword"  title="Send Prescription"
actionListener="#{patientNotePresBackingBean.sendPrescriptionId}"
onclick="return pharmacyCheck('#{row}');"> 
<f:param name="sendPrescriptionId" value="#{patientNotePrescriptionVar.id}" />
<f:param name="processOfPrescription" value="Send" />
<h:graphicImage value="/images/send-prescribe.png" style="border:0;"></h:graphicImage>
</a4j:commandLink>

and my java script is like...

function pharmacyCheck(rowID) {
    var a = document.getElementById("prescriptionId:patientNotePresListId:"+rowID+":pharmacyRcopiaId");
    if(a.value == -1) {
        alert("Please select Pharmacy");
        return false;
    } else {
        return true;
    }

}

Actually when it returns true it should call patientNotePresBackingBean.sendPrescriptionId right?

But it's not calling the same.... Is there any reason why?

When you see the generated HTML, something like this will be in the onclick method of the button/link:

onclick="return pharmacyCheck('#{row}');;A4J.AJAX.Submit(..."

So the onclick is returning at the beginning and the ajax submit will not be sent. To solve your problem, use your javascript method in this way:

<a4j:commandLink onclick="if (!pharmacyCheck('#{row}')) return false;" ...>

EDIT: Further explanation:

When you have a JavaScript code like this:

function X() {
    return false;
    alert("this alert won't be shown =(");
}

As obviously stated, the alert won't fire never. So, checking the generated HTML for onclick (specially formatted to see the similarities):

onclick="
    return functionX();
    alert('this alert will not display either X_x');
    A4J.AJAX.Submit(...
"

The onclick code won't fire because you're returning something before executing the code. The change will just intercept the false value and do a return, in case your method returns true , the rest of the code will be executed.

Note: By default, any javascript method that doesn't return anything, returns false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM