2011-11-24 43 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?')" />