最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return false not working and action gets triggered on click of h:commandButton - Stack Overflow

programmeradmin3浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论