問(wèn)題描述
在我的應(yīng)用程序中,我在某些特定操作上打印了一些文檔以及一些復(fù)選框(默認(rèn)情況下,當(dāng)頁(yè)面加載時(shí),某些復(fù)選框會(huì)被選中).
In my application I am printing some document along with some checkboxes (Some checkboxes get checked by default when page loads) on some specific action.
現(xiàn)在用戶將選中一些復(fù)選框或取消選中一些.并點(diǎn)擊更新按鈕.
Now user will Check some checkboxes Or may uncheck some. And clicks on update button.
現(xiàn)在我的要求是我想要未選中的復(fù)選框值.
例如:
當(dāng)頁(yè)面第一次加載時(shí),有 5 個(gè)復(fù)選框被選中大約 700 個(gè)復(fù)選框中的默認(rèn)值;
When the page loads first time there are 5 checkboxes checked by default out of some 700 check boxes;
現(xiàn)在用戶將取消選中 2 個(gè)復(fù)選框并單擊提交.
now user will uncheck 2 checkboxes and clicks on submit.
我的要求在這里 我想要那 2 個(gè)我的操作類中未選中的復(fù)選框值.
My requirement is here I want those 2 unchecked checkboxes values in my action class.
我正在使用 Struts2 打印這些文檔.我正在使用 Struts2-jQgrid,我嘗試使用 Struts2 CheckboxInterceptor 來(lái)獲取未選中的復(fù)選框,但它給了我所有未選中的復(fù)選框,而不僅僅是所需的復(fù)選框(改變狀態(tài)的復(fù)選框).p>
I am using Struts2 to print those documents. I'm using Struts2-jQgrid, I have tried Struts2 CheckboxInterceptor to get the unchecked ones but it's giving me all the unchecked checkboxes, not only the desired ones (the ones that changed their state).
推薦答案
假設(shè)您從迭代自定義對(duì)象列表開(kāi)始,
Assuming you start by iterating a List of custom objects,
private List<MyCustomObject> myList;
并且您的自定義對(duì)象具有例如.boolean isChecked()
和 String getValue()
方法,
and that your custom object has eg. boolean isChecked()
and String getValue()
methods,
您可以在目標(biāo)操作中使用兩個(gè)不同的列表:一個(gè)用于未選中但現(xiàn)在已選中的項(xiàng)目,另一個(gè)用于已選中但現(xiàn)在未選中的項(xiàng)目強(qiáng)>:
you could use two different Lists in the destination Action: one for the items that were unchecked and now are checked, and another one for items that were checked and now are unchecked:
private List<String> itemsThatHaveBeenChecked;
private List<String> itemsThatHaveBeenUnchecked;
然后在頁(yè)面中,您應(yīng)該使用經(jīng)典復(fù)選框來(lái)獲取選中項(xiàng),并使用隱藏輸入來(lái)存儲(chǔ)未選中項(xiàng)的值,使用 javascript 激活其值,與復(fù)選框值相反(必須沒(méi)有名稱):
then in the page you should use a classic checkbox to get the checked items, and an hidden input to store the value of the unchecked items, activating its value with javascript as opposite to the checkbox value (that must have no name):
<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>
這樣您將只獲得兩個(gè)列表中所需的值.
This way you will get only the values needed in both Lists.
這篇關(guān)于如何在 Struts2 中獲取特定的未選中復(fù)選框的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!