我想要一个可点击的图像,它会在鼠标悬停/移出/单击时发生变化。不用说,我希望在“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
发布评论
评论(1)
(和
)的默认行为是提交父 POST 表单,该表单是由
指定。onclick
属性只允许开发人员在提交表单之前执行一段 JavaScript 代码。默认情况下,它不会阻止表单提交。如果您想阻止按钮的默认行为(提交表单),则需要将return false;
添加到 onclick。如果是
,组件的type
属性默认为submit
并且它将生成一个 HTML。当您将其更改为
button
时,它将变成一个“死”按钮,该按钮基本上不执行任何操作,期望执行附加到它的任何 JavaScript 处理程序,并且它将生成 HTML< ;输入类型=“按钮”>
。当使用组件的image
属性时,它将被转入图像映射,该映射允许您提交光标相对于图像的 X,Y 位置(但是,在您的情况下,您似乎想要只是一个背景图像;在这种情况下,您实际上应该使用 CSSbackground-image
),并且组件的type
属性将被忽略将生成一个 HTML。
这一切都清楚地记录在 标签文档。
如果您的唯一要求是阻止
提交父表单,那么您需要将return false;
添加到onclick,如前所述:另一种方法是将
type="button"
与 CSS 背景图像结合使用(这样您就不会滥用image
属性):在 CSS 文件中包含以下内容,该文件由
: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 addreturn false;
to the onclick.In case of
<h:commandButton>
, the component'stype
attribute defaults tosubmit
and it will generate a HTML<input type="submit">
. When you change it tobutton
, 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'simage
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 CSSbackground-image
instead) and the component'stype
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 addreturn false;
to the onclick, as said before:An alternative is to use
type="button"
in combination with a CSS background image (so that you aren't abusing theimage
attribute):with the following in a CSS file which is loaded by
<h:outputStylesheet>
: