返回false不起作用,并且在单击h:commandButton时触发了操作 [英] Return false not working and action gets triggered on click of h:commandButton

查看:47
本文介绍了返回false不起作用,并且在单击h:commandButton时触发了操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行return false时仍然有一个问题,但该操作仍在发生.我不明白问题是什么.

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

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

JS代码:

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;
                      }
                    }   
                }

现在在上面的JS函数中,我可以清楚地看到警报测试被调用,但是仍然触发了操作.请指导.

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

推荐答案

您实际上应该返回来自onclick属性中的javascript内容.

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

正在更改

onclick="javascript:statusCheck()"

onclick="return statusCheck()"

应该可以解决问题.我之前看到过,有时需要从onclick语句中返回某些内容(有时?).调用(statusCheck())的函数返回某些事实的事实并不意味着此返回值也将进一步返回给commandButton onclick.因此,它始终会继续调用该动作

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 commandButton onclick. So it always continues to call the action

这篇关于返回false不起作用,并且在单击h:commandButton时触发了操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆