【发布时间】:2011-04-26 15:45:32
【问题描述】:
这是我试图完成的。我有一个带有两个 JSF inputText 和一个按钮的隐藏表单。无论我在div 容器内单击什么位置,我都会尝试计算相对于我的容器的x 和y 坐标。然后使用jQuery将x和y的值设置为两个JSF inputText的值并提交表单。 JSF 页面后面的托管 bean 将尝试捕获 x 和 y 提交的值。
以下是我的代码。请让我知道我做错了什么,因为托管 bean myBean 内的方法 createNewInput 没有正确被调用。
EDIT2
<h:outputScript library="primefaces" name="jquery/jquery.js" target="head"/>
<div id="result">Click an Element</div>
<div id="pdfCol">
<h:form>
<h:outputText value="#{note.test}"/>
<p:commandButton actionListener="#{myBean.createNewInput}" value="Submit"/>
<!-- This button is to see when I click, whether the method inside managed bean get invoked. And it did. By looking at the log files.-->
</h:form>
</div>
<div id="noteCol">
<h:form style="display:none;" prependId="false">
<h:inputText id="coordX" value="#{myBean.newInputXCoordinate}"/>
<h:inputText id="coordY" value="#{myBean.newInputYCoordinate}"/>
<p:commandButton id="button" actionListener="#{myBean.createNewInput}" value="Submit"/>
</h:form>
</div>
<script>
jQuery("#noteCol").click(function(e){
var offset = jQuery(this).offset();
e.stopPropagation();
jQuery("#result").text(this.id + "Offset: (" + offset.left + ", " + offset.top + "), " +
"Relative Coordinate: (" + (e.pageX - offset.left) + ", " + (e.pageY - offset.top) + ")");
jQuery("#coordX").val(e.pageX - offset.left);
jQuery("#coordY").val(e.pageY - offset.top);
jQuery("#button").click();
});
</script>
在服务器端,当托管 bean myBean 中的方法 createNewInput() 被调用时,我打印出这对坐标。很高兴,我看到了两个坐标的正确结果。但是,它会一遍又一遍地打印这对坐标,但是在第一次正确打印后,这对坐标的值是NaN。但是我要解决这个问题吗? p>
【问题讨论】:
-
你在
jQuery("#noteCol").click(function(e)...里面有jQuery("#button").click(function(e)...,我不认为那是你想做的。您编写的代码意味着:单击#noteCol 时,将#button 的onclick 事件处理程序更改为新函数。新函数现在不会被调用,只有当你点击#button时才会被调用。 -
@adam: 我该如何解决,当我点击
#noteCol时,#button也会被点击。 -
正如 BalusC 所说,
jQuery("#button").click();应该可以工作。一定要删除jQuery("#button").click(function(e)...。也不要一直改例子,p:commandButton和h:commandButton是不一样的。如果是简单的h:commandButton,你应该使用jQuery("#button").closest("form").submit();,如果是p:commandButton,使用jQuery("#button").click();。 -
@adam:谢谢。现在对我来说更有意义了。我还有一个问题要问你。所以我使用
p:commandButton和jQuery("#button").click()在服务器端,我打印出两个坐标。可以,但是成功打印出第一对坐标后,又一遍遍地继续打印出这些坐标,但是这些坐标的值是NaN。知道为什么以及如何解决这个问题吗,亚当?
标签: jquery jsf primefaces