widgetVar不能在p:fileUpload中使用

时间:2013-01-09 09:47:48

标签: jquery file-upload jsf-2 primefaces widget

我想在选择错误类型的文件时删除错误消息。我按照http://forum.primefaces.org/viewtopic.php?f=3&t=23853链接。

<p:fileUpload fileUploadListener="#{userProfileUpdateController.upload}"         
    widgetVar="fileuplaodWgt"                     
    allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
    description="Select Images"
    update="userPhoto"
    mode="advanced"/>

我导入<h:outputScript name="library.js" library="script"></h:outputScript>

在library.js

$(document).ready(function(){
   alert(fileuplaodWgt);
   fileuplaodWgt.buttonBar.find("button.cancel").bind("click", function(e){
   clearInvalidFileMsg();
    });
});

function clearInvalidFileMsg(){
    fileuplaodWgt.uploadContent.find("tr.ui-state-error").remove();
 }

上面写完后,我的页面中没有显示文件上传。当我跟踪我的library.js文件时,发现 fileuplaodWgt ReferenceError:fileuplaodWgt未定义。我尝试过mozilla和chrome。他们都没有显示我的文件上传。你能帮我解决我的错误吗?

1 个答案:

答案 0 :(得分:1)

PrimeFaces在$(function())上初始化DOM上的小部件变量。包含该命令的<script>直接在组件之后呈现。因此,如果在小部件var之前调用 ,那么它将找不到小部件var,因为它稍后会被初始化。您需要确保在呈现组件后调用<{1>} 。其中一种方法是通过$(document).ready()将脚本重定位到HTML $(document).ready()的末尾。

<body>

这样,在到达文档末尾时加载并初始化脚本,因此所有小部件var初始化之后。

另一种方法是使用target="body"而不是<h:outputScript name="library.js" library="script" target="body" /> ,但这很笨拙并且可能更慢,因为只有在加载文档中引用的所有图像时才会调用它。


对具体问题

无关,您使用资源$(window).load()的方式不对。请仔细阅读What is the JSF resource library for and how should it be used?,然后使用

$(document).ready()

顺便说一下,你还有一个令人不安的小部件var名称错误与问题没有进一步关联。它是上传,而不是 uplaod