admin管理员组

文章数量:1424942

I have an issue when return false is executed still the action takes place. I don't understand what the issue is.

<h:mandButton value="Deny" style="margin-left: 5px;" id="button"                     
action="#{myController.submitRequest('deny', 'return')}"
onclick="javascript:statusCheck();"/>

JS Code:

function statusCheck()
                {
                    var msgArray = [];
                    var validate = true;
                    var status=  document.getElementById('userValue:statusId');

                    if(status.value.trim() == "")
                    {
                        msgArray.push("Status required");
                        alert("check");
                        validate = false;
                    }
                    alert("after check");
                    alert(validate);
                    if(validate) {
                        return true;
                    }
                    else
                    {
                      if(msgArray.length >0)
                      {
                            alert(msgArray.join('\n'));
                            alert("test");
                            return false;
                      }
                    }   
                }

Now in the above JS function i can clearly see the alert test gets called but still the action is triggered. Please guide.

I have an issue when return false is executed still the action takes place. I don't understand what the issue is.

<h:mandButton value="Deny" style="margin-left: 5px;" id="button"                     
action="#{myController.submitRequest('deny', 'return')}"
onclick="javascript:statusCheck();"/>

JS Code:

function statusCheck()
                {
                    var msgArray = [];
                    var validate = true;
                    var status=  document.getElementById('userValue:statusId');

                    if(status.value.trim() == "")
                    {
                        msgArray.push("Status required");
                        alert("check");
                        validate = false;
                    }
                    alert("after check");
                    alert(validate);
                    if(validate) {
                        return true;
                    }
                    else
                    {
                      if(msgArray.length >0)
                      {
                            alert(msgArray.join('\n'));
                            alert("test");
                            return false;
                      }
                    }   
                }

Now in the above JS function i can clearly see the alert test gets called but still the action is triggered. Please guide.

Share Improve this question edited Dec 18, 2015 at 12:17 Kukeltje 12.3k4 gold badges26 silver badges47 bronze badges asked Dec 18, 2015 at 11:39 sTgsTg 4,44418 gold badges74 silver badges125 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

You should actually return something from the javascript that is inside the onclick attribute.

So changing

onclick="javascript:statusCheck()"

to

onclick="return statusCheck()"

should do the trick. I've seen before that it is (sometimes?) actually needed to return something from the statement in the onclick. The fact that the function that is called (statusCheck()) returns something does not mean this return value is also returned further to the mandButton onclick. So it always continues to call the action

本文标签: javascriptReturn false not working and action gets triggered on click of hcommandButtonStack Overflow