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 01 Answer
Reset to default 6You 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
版权声明:本文标题:javascript - Return false not working and action gets triggered on click of h:commandButton - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745382348a2656221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论