簡體   English   中英

如何在Struts2中獲取未選中的特定復選框

[英]How to get the specifc unchecked checkboxes in Struts2

在我的應用程序中,我在某些特定操作上打印一些文檔以及一些復選框(頁面加載時默認情況下會選中某些復選框)。

現在,用戶將選中某些復選框,或者可以取消選中某些復選框。 並單擊更新按鈕。

現在,我的要求是我希望未選中的復選框值。

例如 :

首次加載頁面時,默認情況下,約700個復選框中有5個復選框處於選中狀態;

現在,用戶將取消選中2個復選框,然后單擊“提交”。

我的要求是在這里,我希望動作類中的這兩個未選中的復選框值。

我正在使用Struts2打印這些文檔。 我正在使用Struts2-jQgrid,我已經嘗試過Struts2 CheckboxInterceptor來獲取未選中的復選框,但是它為我提供了所有未選中的復選框,不僅是我想要的復選框( 更改了狀態的復選框)。

假設您從迭代自定義對象列表開始,

private List<MyCustomObject> myList;

並且您的自定義對象具有例如。 boolean isChecked()String getValue()方法,

您可以在目標Action中使用兩個不同的List:一個用於未選中但現在已選中項目,另一個用於已選中但現在未選中的項目

private List<String> itemsThatHaveBeenChecked;
private List<String> itemsThatHaveBeenUnchecked;

然后在頁面中,您應該使用經典的復選框來獲取已選中的項目,並使用隱藏的輸入來存儲未選中的項目的值,使用與復選框值相反的javascript激活其值(必須沒有名稱):

<s:iterator value="myList" status="ctr">
    <s:if test="!isChecked()">
        <!-- I'm not checked, need to catch only if I will be checked -->
        <s:checkbox id = "checkbox_%{#ctr.index}" 
            fieldValue = "%{getValue()}"
                 value = "false" 
                  name = "itemsThatHaveBeenChecked" />
    </s:if>
    <s:else>
        <!-- I'm checked, need to catch only if I will be unchecked -->
        <input type = "checkbox" 
                 id = "<s:property value="%{'checkbox_'+#ctr.index}"/>"
            checked = "checked"
              class = "startedChecked" />
        <!-- hidden trick -->
        <input type = "hidden" 
                 id = "hidden_<s:property value="%{'checkbox_'+#ctr.index}"/>" 
              value = "<s:property value="%{getValue()}"/>"         
               name = "itemsThatHaveBeenUnchecked" 
           disabled = "disabled" />            
    </s:else> 
</s:iterator>

<script>
    /* Enable the hidden field when checkbox is unchecked, 
      Disable the hidden field when checkbox is checked    */
    $(function() {
        $("input[type='checkbox'].startedChecked").change(function () {
            $("#hidden_"+this.id).prop({disabled : this.checked});
        });
    });
</script>

這樣,您將只獲得兩個列表中所需的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM