简体   繁体   中英

jquery checkbox - get the value of only the checked checkbox

 $(function() { $('input[type=checkbox]').change( function() { if ($(this).is(':checked')) { document.getElementById("chk_option").value += this.value + ","; alert($("#chk_option").val()); } else { document.getElementById("chk_option_remove").value += this.value + ","; } } ); }); 
 <div> <center> <form id="form_tarif" class="form-horizontal" style="padding-top:57px;" action="pricesinput" method="POST"> <input type="hidden" id="chk_option" name="chk_option"> <input type="hidden" id="chk_option_remove" name="chk_option_remove"> <c:forEach items="${option_tarif_list}" var="option_tarif" varStatus="loop"> <div class="checkbox1"> <label> <input type="checkbox" name="tarif_inclue[]" value="${option_tarif.id}" class="checkboxchk" id="option_tarif_chk_${option_tarif.id}">${option_tarif.libelle} </label> </div> </c:forEach </form> </center> </div> 

Hi,

Can you please help me with the following JS. I need to take the values of only the checked checkbox and use it in the id #chk_option. I will then use this id in my Java code for insertion into the database.

Here is part of my java code below:

String currentTypeTarifIdStr = request.getParameter("current_typetarif_id");
        Integer currentTypeTarifId = Integer.valueOf(currentTypeTarifIdStr);

        String TypeTarifIdToRemoveStr = request.getParameter("typetarif_id_to_remove");
        Integer TypeTarifIdToRemove = Integer.valueOf(TypeTarifIdToRemoveStr);

        Integer trimmedOptionSplit = 0;
        String optionChkStr = request.getParameter("chk_option");
        String[] optionSplitStrs = optionChkStr.split(",");


        String TypeTarifOptionToRemoveStr = request.getParameter("typetarif_option_to_remove");
        Integer TypeTarifOptionToRemove = Integer.valueOf(TypeTarifOptionToRemoveStr);





                        query = mEntityManager.createQuery("DELETE FROM TarifTypeOptionEntity WHERE typeTarifId=:pId");
                        int deletedCount = query.setParameter("pId", currentTypeTarifId).executeUpdate();
                        mUserTransaction.commit();


                        for (String optionSplitStr : optionSplitStrs) {
                            String trimmedOptionSplitStr = optionSplitStr.trim();
                            // do something with trimmedOptionSplitStr
                            if (!trimmedOptionSplitStr.equals("")) {
                                trimmedOptionSplit = Integer.valueOf(trimmedOptionSplitStr);
                            }

                            mUserTransaction.begin();
                            TarifTypeOptionEntity typeTarifTypeOption = new TarifTypeOptionEntity();
                            typeTarifTypeOption.setTypeTarifId(currentTypeTarifId);
                            typeTarifTypeOption.setTarifOptionId(trimmedOptionSplit);
                            mEntityManager.persist(typeTarifTypeOption);
                        mUserTransaction.commit();
                        }

                    mUserTransaction.begin();
                    }

I will first delete everything in the database, then insert only the checked checkbox in the database. To do so, i should concatenate all the checked values with a comma. Then in my java code, i will split it and remove the commas.

But it's not happening. can someone please help me with the coding and spot what i'm doing wrong.

EDITS:

No error in the console and the alert does work. Infact this code works perfectly. On clicking the checkbox the if clause is executed and each time i check another checkbox the value is added following a comma. This is those values that i will need in java. But it's not exacly what i need from this code.

Right now it adds only the value i unchecked from the checkbox into the database. But ofcourse this can't happen because this value is already present, . so i get the error Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails ( locforall_booking . t_be_tarif_type_option , CONSTRAINT fk_t_be_type_tarif_option_t_be_tarif_option1 FOREIGN KEY ( be_tarif_option_id ) REFERENCES t_be_tarif_option ( id ) ON DELETE CASCA)

Scenario of my problem: If while i have 2 values checked and i don't uncheck it but check a third value, it will delete everything in the database and only keep that third value. But what i actually need is for it to delete everything in the database and keep all values including the third one.

Any Javascript expert around who can help please?

You can perform this using 2D array as:

<html>

    var items = [["abc.jpg", "xyz.jpg"],["123.jpg","345.jpg"]];
    alert(items[1][1]); // 345.jpg
</script>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM