简体   繁体   English

jQuery复选框-仅获取已选中复选框的值

[英]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. 您能否通过以下JS帮助我。 I need to take the values of only the checked checkbox and use it in the id #chk_option. 我只需要获取选中复选框的值,并在ID #chk_option中使用它。 I will then use this id in my Java code for insertion into the database. 然后,我将在Java代码中使用此ID插入数据库。

Here is part of my java code below: 这是下面我的Java代码的一部分:

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. 然后在我的Java代码中,我将其拆分并删除逗号。

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. 在单击复选框时,将执行if子句,每次我选中另一个复选框时,该值都会在逗号后面添加。 This is those values that i will need in java. 这是我在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) 所以我得到的错误造成的: 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. 我的问题的场景:如果当我检查了2个值并且我没有取消选中它而是检查了第三个值时,它将删除数据库中的所有内容并仅保留该第三个值。 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? 有谁可以帮助您的Java语言专家?

You can perform this using 2D array as: 您可以使用2D数组执行以下操作:

<html>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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