javascript JSF 标签上的 onclick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15094261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onclick event on JSF tag
提问by gabriel angelos
How to bind JavaScript onclickevent to JSF-tag?
Here's my JSF tag:
如何将 JavaScriptonclick事件绑定到 JSF 标签?这是我的 JSF 标签:
<h:commandLink value="Edit" action="#{adminBean.edit}">
<f:param value="#{user.login}" name="login"/>
</h:commandLink>
I've managed to bind click-event on a JSP-tag JavaScript function:
我设法在 JSP-tag JavaScript 函数上绑定了点击事件:
<script type="text/javascript">
function deleteUser(link)
{
if (confirm("Are you sure?")) {
window.location = link;
}
else {
}
}
</script>
Here's my link example in JSP-tag.
这是我在 JSP-tag 中的链接示例。
out.println("<a href=\"#\" onclick=\"deleteUser('deleteUser.htm?userLogin="+user.getLogin()+"');\">Delete</a>");
How can I bind the same on-click function to given above JSF-tag ?
如何将相同的点击功能绑定到上面给出的 JSF-tag ?
回答by Luiggi Mendoza
Use the onclickattribute in the <h:commandLink>tag component:
使用标签组件中的onclick属性<h:commandLink>:
JSF code
JSF代码
<h:commandLink value="Edit" action="#{adminBean.edit}"
onclick="if (!deleteUser()) return false;">
<f:param value="#{user.login}" name="login"/>
</h:commandLink>
And change your script to
并将您的脚本更改为
<script type="text/javascript">
function deleteUser(link) {
return confirm("Are you sure?");
}
</script>
By the way, I think you have confused the terms editand delete, but that's outside the question scope.
顺便说一句,我认为您混淆了术语edit和delete,但这不在问题范围内。

