0

我有下一个代码:

<h:form name="frm">
<h:panelGroup>          
<p:commandButton value="Good" id='good' action="#{job.SetGood}" accesskey="g"/>
<p:commandButton value="Bad" id='bad' action="#{job.SetBad}" accesskey="b"/>
</h:panelGroup>

我需要用户可以只使用键 Space 来“按下”id='good' 的按钮,并使用键“backspace”来“按下”id='bad' 的按钮我尝试了很多东西但没有工作.. . 例如,我尝试在加载时调用函数 cargar() 并且不工作......有人可以帮助我吗?

function cargar()
{
document.getElementById("good").focus();
document.onkeypress=function(e)
{
var esIE=(document.all);
var esNS=(document.layers);
tecla=(esIE) ? event.keyCode : e.which;
if(tecla==32)
{
frm.submit();
 <!--Also I try: document.getElementById("good").click();-->
}
};
}
4

2 回答 2

0

你可以做到这一点。

<h:form name="frm">
   <p:remoteCommand name="executeAction" listener="#{job.SetGood}" update="frm" />
  <h:panelGroup>          
     <p:commandButton value="Good" id='good' 
          accesskey="g" onfocus="executeAction();" />
     <p:commandButton value="Bad" id='bad' action="#{job.SetBad}" accesskey="b"/>
  </h:panelGroup>
</h:form>

<p:remoteCommand将在您<p:commandButton获得焦点时执行。

于 2013-10-26T07:20:26.450 回答
0

尝试设置属性 prependId:

<h:form name="frm" prependId="false">

这将使按钮像

<button id="good" name="good" ...

如果您使用保持默认值 (prependId="true"),则按钮将呈现为

<button id="j_idt14:good" name="good" ...

在这种情况下,j_idt14 是父表单组件的 id。

javascript 函数可能不会在您的代码中找到元素document.getElementById("good")

于 2013-10-25T20:32:38.517 回答