h:commandLink 和 onclick Javascript 函数
我有以下和平的代码:
<DIV>
<h:commandLink action="#{documentGen.generatePDF}"
target="_blank"
onclick="return checkSelected();">
<h:graphicImage url="/images/pdf.jpg"
styleClass="pdfimage" title="Click to show PDF"></h:graphicImage>
</h:commandLink>
</DIV>
上面的内容放在一个表格中,用户可以在其中勾选复选框并单击上面的 commandLink,这意味着打开一个新窗口并显示由 backbean #{documentGen 生成的内容.generatePDF}
。
如果我不包含onclick="return checkSelected();"
,这将按预期工作。如果我包含它,那么 backbean 永远不会被调用,而是我得到一个与当前窗口重复的新窗口。
我需要这个 javascript 函数来检查用户至少选中了 1 个复选框,否则显示警报。 Javascript 函数工作正常,如果单击 commandLink 时没有勾选复选框,我会看到警报。
我的问题是,为什么 onclick
行会搞乱 commandLink 的功能?我做错了什么?
更新:
h:commandLink
位于h:form
内。如前所述,上面的命令链接可以按预期工作,无需 onclick=....
。
为了测试,我将 onclick 上调用的 javascript 函数更改为:
function testFunc() {
alert("GOT Here!")
return ture;
}
并在单击的命令链接中更改为: onclick="return testFunc();"
结果是我收到警报“GOT Here!”但和以前一样,新窗口与当前窗口重复,并且 backbean 不会被调用。删除 onclick 行可以纠正结果(但需要那里的 javascript 函数来检查选中的复选框)。
在浏览器源代码视图中,不带 onclik 的命令链接和带 onclick 的命令链接之间的区别如下:
<a href="#" onclick="return oamSubmitForm('genView','genView:_idJsp33','_blank',[]);" id="genView:_idJsp33" target="_blank"><img src="/images/pdf.jpg" title="Click for Pdf Document" class="pdfimage"></a>
<a href="#" onclick="return testFunc();;return oamSubmitForm('genView','genView:_idJsp33','_blank',[]);" id="genView:_idJsp33" target="_blank"><img src="/images/pdf.jpg" title="Click for Pdf Document" class="pdfimage"></a>
任何帮助将不胜感激。谢谢
I have the following peace of code:
<DIV>
<h:commandLink action="#{documentGen.generatePDF}"
target="_blank"
onclick="return checkSelected();">
<h:graphicImage url="/images/pdf.jpg"
styleClass="pdfimage" title="Click to show PDF"></h:graphicImage>
</h:commandLink>
</DIV>
This above is placed in a table, where user can tick checkboxes and click the above commandLink which is to meant to open up a new window and display the content generated by the backbean #{documentGen.generatePDF}
.
This works as expected if i DON'T include onclick="return checkSelected();"
. IF i include it then the backbean is never called, instead i get new window which is a duplicate of the current.
I need this javascript function to check user has at least checked 1 checkbox and otherwise display alert. The Javascript function works fine and i see the alert if no checkbox is ticked when the commandLink is click.
My question is, why does it onclick
line mess up what commandLink does? What am i doing wrong?
UPDATE:
The h:commandLink
is inside h:form
. As mentioned the above command link works as expected WITHOUT the onclick=....
.
To test i changed the javascript function called on onclick to:
function testFunc() {
alert("GOT Here!")
return ture;
}
and in the commandlink on click to : onclick="return testFunc();"
Result is i get the alert "GOT Here!" but as before, the new window is duplicate of current window and backbean doesn't get called. Removing the onclick line corrects the outcome (but need the javascript function there to check for checked checkboxes).
In browser source view the difference between the commandlink without onclik and with onclick is as below:
<a href="#" onclick="return oamSubmitForm('genView','genView:_idJsp33','_blank',[]);" id="genView:_idJsp33" target="_blank"><img src="/images/pdf.jpg" title="Click for Pdf Document" class="pdfimage"></a>
<a href="#" onclick="return testFunc();;return oamSubmitForm('genView','genView:_idJsp33','_blank',[]);" id="genView:_idJsp33" target="_blank"><img src="/images/pdf.jpg" title="Click for Pdf Document" class="pdfimage"></a>
Any help would be appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是你总是要回来。我认为你想要的只是在必要时返回 false ,否则继续。
因此,将 onclick 更改为
onclick="if (checkSelected()) { return false; }"
这肯定是 Ajax 提交表单的问题,因为如果您之前返回,则永远不会调用提交。我可以想象这对于标准表单提交也很重要。
The problem is that you are always returning. I think what you want is only return false if necessary and continue otherwise.
So change your onclick to
onclick="if (checkSelected()) { return false; }"
This is for sure an issue with Ajax submitted forms, because the submit is never called if you return before. I can imagine that this counts for standard form submits too.
如果在 commandLink 的 onclick 事件中返回 false,则不会提交表单
If you return false in the onclick event of a commandLink, the form won't be submitted