2011-11-24 45 views
0

希望你们一切都会好的。 scenerio是我有一个页面,你上传图片,然后将其删除。我用这样的代码<p:commandLink>在我使用onClick事件时不起作用

<h:panelGrid columns="5" 
       border="" 
       width="20%" 
       style="position: absolute; top: 50px;" 
       columnClasses="asteriskColumns, nameColumns" > 

    <h:outputText value="*" /> 
    <h:outputText value="Map: " /> 
    <p:fileUpload id="cityMap" 
        description="Image" 
        update="city messages" 
        allowTypes="*.jpg;*.png;*.gif;*.jpeg;" 
        auto="true" 
        fileUploadListener="#{cityDetail.imageUpload}" > 

    </p:fileUpload> 

    <p:graphicImage id="city" 
        value="#{cityDetail.imagePath}" 
        width="80" 
        height="50" 
        cache="false"> 

     <f:event type="preRenderComponent" listener="#{cityDetail.putImage}" /> 

    </p:graphicImage> 

    <p:commandLink update="city" 
        action="#{cityDetail.removeImage}" 
        style="color: #0d5b7f;text-decoration: underline" 
        onclick=""> 

     <h:outputText value="remove" /> 

    </p:commandLink> 

    <h:outputText value="*" /> 
    <h:outputText value="Image1: " /> 
    <p:fileUpload id="cityImage1" 
        description="Image" 
        update="Image1 messages" 
        allowTypes="*.jpg;*.png;*.gif;*.jpeg;" 
        auto="true" 
        fileUploadListener="#{cityDetail.imageUpload}" > 

    </p:fileUpload> 

    <p:graphicImage id="Image1" 
        value="#{cityDetail.imagePath}" 
        width="80" 
        height="50" 
        cache="false" > 

     <f:event type="preRenderComponent" listener="#{cityDetail.putImage}" /> 

    </p:graphicImage> 

    <p:commandLink update="Image1" 
        action="#{cityDetail.removeImage}" 
        style="color: #0d5b7f;text-decoration: underline" 
        onclick="if (! confirm('Are you sure, you want to delete the picture?')) { return false;}; return true; "> 

     <h:outputText value="remove" /> 

    </p:commandLink> 

    <h:outputText value="*" /> 
    <h:outputText value="Image2: " /> 
    <p:fileUpload id="cityImage2" 
        description="Image" 
        update="Image2 messages" 
        allowTypes="*.jpg;*.png;*.gif;*.jpeg;" 
        auto="true" 
        fileUploadListener="#{cityDetail.imageUpload}" > 

    </p:fileUpload> 

    <p:graphicImage id="Image2" 
        value="#{cityDetail.imagePath}" 
        width="80" 
        height="50" 
        cache="false" > 

     <f:event type="preRenderComponent" listener="#{cityDetail.putImage}" /> 

    </p:graphicImage> 

    <p:commandLink update="Image2" 
        action="#{cityDetail.removeImage}" 
        style="color: #0d5b7f;text-decoration: underline" 
        onclick="if (! confirm('Are you sure, you want to delete the picture?')) { return false;}; return true; "> 

     <h:outputText value="remove" /> 

    </p:commandLink> 

.....

的问题是每一件事工作正常,如果我使用

的onclick = “”

但是,当我使用

onclick =“if(!confirm ('你确定,你想删除图片吗?')){return false;};返回true; “

然后删除不行,为什么呢?请告诉我,我做错了。

我使用PrimeFaces 2.2 感谢

回答

1

我没有使用号码:commandLink。虽然我做到了以这种方式,它是工作的罚款

<h:outputText value="*" /> 
<h:outputText value="Map: " /> 
<p:fileUpload id="cityMap" 
       description="Image" 
       update="city messages" 
       allowTypes="*.jpg;*.png;*.gif;*.jpeg;" 
       auto="true" 
       fileUploadListener="#{cityDetail.imageUpload}" > 

</p:fileUpload> 

<p:graphicImage id="city" 
       value="#{cityDetail.imagePath}" 
       width="80" 
       height="50" 
       cache="false"> 

    <f:event type="preRenderComponent" listener="#{cityDetail.putImage}" /> 

</p:graphicImage> 

<h:commandLink value="remove" 
       title="Remove Picture" 
       style="color: #0d5b7f;text-decoration: underline" 
       onclick="if (! confirm('Are you sure, you want to remove picture?')) { return false;}; return true; "> 

    <f:ajax event="click" 
      render="city" 
      listener="#{cityDetail.removeImage}"/> 

</h:commandLink> 

感谢

1

这看起来很奇怪,因为你正在尝试这种( 。那种)在Java中的方式,但你复杂onclick="if (! confirm('Are you sure, you want to delete the picture?')) { return false;}; return true; " - 你没有正确使用JavaScript的confirm功能 你可以写:

onclick="return confirm('Are you sure, you want to delete the picture?')" 

,但由于您使用的Primefaces(顺便说一句 - 你可以升级到3.0.M4),你可以使用confirmDialog组件:

<p:commandButton value="Delete picture" onclick="confirmation.show()" type="button"/> 
<p:confirmDialog message="Are you sure, you want to delete the picture?" 
      showEffect="bounce" hideEffect="explode" 
      header="Initiating deleting process" severity="alert" widgetVar="confirmation"> 

    <p:commandButton value="Yes Sure" update="<some component you want to update>" oncomplete="confirmation.hide()" actionListener="#{myBean.deleteMethod}" /> 
    <p:commandButton value="Not Yet" onclick="confirmation.hide()" type="button" /> 

</p:confirmDialog> 

这的确是越写,但它肯定更优雅,漂亮!

1

返回真假并不需要在那里 只是用简单的下面的代码

onclick="return confirm('Are you sure, you want to delete the picture?')" 
0

如果您在该标签,你会发现AJAX使用p:commandLink;设置ajax=false,那么它会正常工作。

3

我面临同样的问题,并设置ajax = false不适合我。

我找到的解决方案是改变onclickonstart

似乎primefaces使用onclick来处理ajax操作,所以当onclick返回一个值(或者是)时,整个操作会中止。

with onstart如果它返回true,则动作继续执行,如果为false,则中止。

2

做这样的:

<p:commandLink update="Image1" action="#{cityDetail.removeImage}" 
    onstart="return confirm('Are you sure, you want to delete the picture?')" />