JSF 命令按钮“onclick”事件不持久

发布于 2025-01-03 13:56:18 字数 641 浏览 1 评论 0 原文

我想要一个可点击的图像,它会在鼠标悬停/移出/单击时发生变化。不用说,我希望在“onclick”之后页面不会重新加载。
我正在尝试使用 h:image 或 h:commandButton 来完成此操作 - 两者都无法按预期工作。 最烦人的是这个: 我的代码 bean 中有以下简单功能:

public String getClick()
{
    return "alert('abc')";
}

在 jsp 文件中,有以下代码:

<td><h:commandButton id="a" type="button" value="click" onclick="#{CodeBean.click}" /></td>
<td><h:commandButton id="b" type="button" image="/resources/empty.jpg" onclick="#{CodeBean.click}"  />

单击后,两者都会引发警报框,但是,只有第二个重新加载页面,而第一个则不会。不知道我与它有什么关系,但是当我查看编译页面的源代码时,我看到第一个按钮的类型保持“按钮”,但第二个按钮变成“图像”。

有什么建议吗? 干杯, 埃雷兹

i want to have a click-able images, which change on roll-over/out/click. needless to say, i want that after the 'onclick' the page won't reload.
i'm trying to accomplish this with either h:image or h:commandButton - both don't work as expected.
the most annoying this is this:
i have in my code-bean the following simple function:

public String getClick()
{
    return "alert('abc')";
}

and in the jsp file, the following code:

<td><h:commandButton id="a" type="button" value="click" onclick="#{CodeBean.click}" /></td>
<td><h:commandButton id="b" type="button" image="/resources/empty.jpg" onclick="#{CodeBean.click}"  />

upon click, both raise the alert box, however, only the second one reloads the page, while the first one doesn't. don't know what i has to do with it, but when i look at the compiled page's source, i see that the first button's type stays 'button', but the second one is turned into 'image'.

any suggestions?
cheers,
eRez

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2025-01-10 13:56:18

(和 )的默认行为是提交父 POST 表单,该表单是由指定。

onclick 属性只允许开发人员在提交表单之前执行一段 JavaScript 代码。默认情况下,它不会阻止表单提交。如果您想阻止按钮的默认行为(提交表单),则需要将 return false; 添加到 onclick。

如果是 ,组件的 type 属性默认为 submit 并且它将生成一个 HTML 。当您将其更改为 button 时,它将变成一个“死”按钮,该按钮基本上不执行任何操作,期望执行附加到它的任何 JavaScript 处理程序,并且它将生成 HTML < ;输入类型=“按钮”>。当使用组件的 image 属性时,它将被转入图像映射,该映射允许您提交光标相对于图像的 X,Y 位置(但是,在您的情况下,您似乎想要只是一个背景图像;在这种情况下,您实际上应该使用 CSS background-image ),并且组件的 type 属性将被忽略将生成一个 HTML

这一切都清楚地记录在 标签文档

如果您的唯一要求是阻止 提交父表单,那么您需要将 return false; 添加到onclick,如前所述:

<h:commandButton image="/resources/empty.jpg" onclick="#{CodeBean.click}; return false;" />

另一种方法是将 type="button" 与 CSS 背景图像结合使用(这样您就不会滥用 image 属性):

<h:commandButton type="button" styleClass="emptyButton" onclick="#{CodeBean.click}" />

在 CSS 文件中包含以下内容,该文件由

.emptyButton {
    background-image: url(#{resource['empty.jpg']});
}

The default behaviour of the <h:commandButton> (and <h:commandLink>) is to submit the parent POST form which is specified by <h:form>.

The onclick attribute just allows the developer to execute some piece of JavaScript code right before the form is been submitted. It does by default not block the form submit. If you'd like to block the button's default behaviour (submitting the form), then you'd need to add return false; to the onclick.

In case of <h:commandButton>, the component's type attribute defaults to submit and it will generate a HTML <input type="submit">. When you change it to button, then it will be turned in a "dead" button which does basically nothing, expect of executing any JavaScript handlers attached to it, and it will generate a HTML <input type="button">. When using the component's image attribute, then it will be turned in an image map which allows you to submit the X,Y position of the cursor relative to the image (however, in your case you seem to want just a background image; in that case you should actually be using CSS background-image instead) and the component's type attribute will be ignored and it will generate a HTML <input type="image">.

This is all clearly documented in Encode Behavior section of the tag documentation.

If your sole requirement is to block the <h:commandButton type="image"> from submitting the parent form, then you need to add return false; to the onclick, as said before:

<h:commandButton image="/resources/empty.jpg" onclick="#{CodeBean.click}; return false;" />

An alternative is to use type="button" in combination with a CSS background image (so that you aren't abusing the image attribute):

<h:commandButton type="button" styleClass="emptyButton" onclick="#{CodeBean.click}" />

with the following in a CSS file which is loaded by <h:outputStylesheet>:

.emptyButton {
    background-image: url(#{resource['empty.jpg']});
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文