Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

javascript - How to get the specifc unchecked checkboxes in Struts2

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.

Now user will Check some checkboxes Or may uncheck some. And clicks on update button.

Now my requirement is I want the checkboxes values that are unchecked.

For example :

When the page loads first time there are 5 checkboxes checked by default out of some 700 check boxes;

now user will uncheck 2 checkboxes and clicks on submit.

My requirement is here I want those 2 unchecked checkboxes values in my action class.

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming you start by iterating a List of custom objects,

private List<MyCustomObject> myList;

and that your custom object has eg. boolean isChecked() and String getValue() methods,

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;

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>

This way you will get only the values needed in both Lists.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...