【问题标题】:Use jQuery to generate PrimeFaces button clicked, but the click event get called over and over使用jQuery生成PrimeFaces按钮被点击,但点击事件被一遍又一遍地调用
【发布时间】:2011-04-26 15:45:32
【问题描述】:

这是我试图完成的。我有一个带有两个 JSF inputText 和一个按钮的隐藏表单。无论我在div 容器内单击什么位置,我都会尝试计算相对于我的容器的xy 坐标。然后使用jQuery将xy的值设置为两个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:commandButtonh:commandButton 是不一样的。如果是简单的h:commandButton,你应该使用jQuery("#button").closest("form").submit(); ,如果是p:commandButton,使用jQuery("#button").click();
  • @adam:谢谢。现在对我来说更有意义了。我还有一个问题要问你。所以我使用p:commandButtonjQuery("#button").click() 在服务器端,我打印出两个坐标。可以,但是成功打印出第一对坐标后,又一遍遍地继续打印出这些坐标,但是这些坐标的值是NaN。知道为什么以及如何解决这个问题吗,亚当?

标签: jquery jsf primefaces


【解决方案1】:

我不确定你为什么会收到NaN。你在哪里看到的?此外,如果您计算 e.pageX - offset.left 而不是相反,这对我来说更有意义。此外,在 jQuery 中设置输入元素的值应该由 jQuery.val() 函数完成。

此外,JSF 将在生成的 HTML 输入元素 ID 前加上表单 ID,这将导致 jQuery('#coordX') 不返回任何内容。 jQuery 采用 HTML 元素 ID,而不是 JSF 组件 ID。您想将prependId="false" 添加到&lt;h:form&gt; 以禁用它。但是您应该确保相同的输入组件不会在输出中出现一次以上,并且您在输入组件上没有任何其他父 UINamingContainer 组件也会添加它们的 ID,以验证您需要检查生成的 HTML &lt;input type="text"&gt; 是否确实具有 id="coordX",因为您尝试通过 jQuery 选择。

最后,jQuery.submit() 仅适用于 HTML &lt;form&gt; 元素,不适用于按钮。为此,您宁愿使用jQuery.click()

所以,总结一下这在理论上应该对你有用:

<div id="noteCol">
    <h:form prependId="false" style="display:none;">
        <h:inputText id="coordX" value="#{myBean.newInputXCoordinate}"/>
        <h:inputText id="coordY" value="#{myBean.newInputYCoordinate}"/>
        <h:commandButton id="button" actionListener="#{myBean.createNewInput}"/>
    </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 + ", " + e.pageY + ")");

        jQuery("#coordX").val(e.pageX - offset.left);
        jQuery("#coordY").val(e.pageY - offset.top);

        jQuery("#button").click();                
        // Or:
        // jQuery("#button").closest("form").submit();
    });
</script>

【讨论】:

  • 我得到NaN的原因是因为我尝试做类似Jquery("#result").text("..." + e.pageX - offset.left + "...")的事情,我忘了把()放在e.pageX - offset.left周围。但是我仍然无法在我的托管 bean 中调用该方法。我添加了这个jQuery("#button").submit(function(e){ alert("Button submit"); }); 以查看button 是否获得提交,但我没有看到任何警报框出现。知道为什么吗,BalusC?
  • 顺便说一句。我确实检查了生成的 HTML,并且有 id 为 coordXcoordYbutton 的组件。我稍微改变了我的代码。我重新更新了我的原始帖子以反映这一点。
  • submit() 仅适用于 &lt;form&gt;。在&lt;input type="submit"&gt; 上,您更愿意使用click()
  • 我实际上也尝试过。我添加了一个回调函数,该函数将在单击按钮时调用一个警报框(请参阅我在原始帖子中编辑的代码)。警告框永远不会出现。我从表格中取出style="display:none",我看到inputText 的值设置正确。如果我手动单击该按钮,则会出现警告框。太奇怪了。
  • 哦,现在是p:commandButton 而不是h:commandButton?你真的有什么?当按钮启用 ajax 时,form.submit() 将不起作用。使用button.click(),不带任何警报功能。就这样jQuery("#button").click();。否则,您基本上会覆盖按钮上的 PrimeFaces 默认 ajax onclick 函数。
【解决方案2】:

这是我的问题的解决方案。问题是点击事件冒泡了。所以为了阻止这个

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();
});

jQuery("#button").click(function(e){
    e.stopPropagation();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2014-12-28
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多