【问题标题】:jquery (.on) does not work with primefaces .ui-fileupload-cancel, why?jquery (.on) 不适用于 primefaces .ui-fileupload-cancel,为什么?
【发布时间】:2021-03-23 00:04:15
【问题描述】:

我正在使用 primefaces 8.0 和 jsf 2.2,并尝试使用 jquery $ 函数选择 .ui-fileupload-cancel 元素并收听 (.on) 但它不起作用,当我使用 (.click) 时它只能工作第一次点击。

<ui:composition template="/templates/main.xhtml">

<ui:define name="body">

<script>

    $(document).ready(function(){
        $(".ui-fileupload-cancel").click(function(){
                alert("Works");
        });
    });
                
    $(document).ready(function() {
       $(".ui-fileUpload").on("click",".ui-fileupload-cancel",function() {
            alert("Does not work");
        });
    })
                   
</script>

        <p:accordionPanel id="uploadPanel" activeIndex="null">

            <p:tab id="uploadTab" title="title">
    
                <p:fileUpload 
                    id="upload" listener="#{Controller.uploadListener}"
                    mode="advanced"  update="messages @this"
                    allowTypes="/(\.|\/)(xml)$/" fileLimit="1"
                    oncomplete="uploadcomplete()" onerror="uploadcomplete()" />

                <p:remoteCommand name="uploadcomplete" update="someId anotherId"
                    process="@this" />

                <p:remoteCommand name="doClear"
                    action="#{Controller.doClear}" update="someId anotherId"
                    process="@this" />

                <p:growl id="messages" showDetail="false" />
            </p:tab>
        </p:accordionPanel>

从 primefaces 文件上传文档中剥皮。

类 适用

.ui-fileupload 主容器元素

.ui-fileupload-buttonbar 按钮栏

.ui-fileupload-choose 浏览按钮

.ui-fileupload-upload 上传按钮

.ui-fileupload-cancel 取消按钮

.ui-fileupload-content 内容容器

我错过了什么?

【问题讨论】:

    标签: jquery jsf primefaces jsf-2.2


    【解决方案1】:

    将事件添加到文档而不是 .ui-fileupload

     $(document).on("click",".ui-fileupload-cancel",function() {
            alert("Does not work");
        });
    

    【讨论】:

    • 在 jsf 中的 body 标签,看起来像这样 ,重要吗?
    • 试试 $(document).on
    • 还是一样,但是当我使用另一个元素来测试它时,我使用了“#uploadPanel”和 $(document),它起作用了!!但使用“”.ui-fileupload-cancel”仍然没有响应。
    • 以上答案应该有效。原因是因为 Jquery Event Delegation 并且因为在呈现页面时取消按钮不存在。看这里的解释:stackoverflow.com/questions/15092578/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2023-03-10
    • 1970-01-01
    • 2017-11-04
    相关资源
    最近更新 更多