vendredi 7 août 2015

jquery checkbox - get the value of only the checked checkbox

$(function() {
  $('input[type=checkbox]').change(
    function() {
      // $("#chk_option").val(this.value + ",") + $("#chk_option").val(this.value)  ;alert($("#chk_option").val());

      //document.getElementById("chk_option").value = "";

      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();
                    }

Full Java code

-the part concerning me is the (modification mode) -a debug shows that the optionChkStr is "" . So i'm guessing it has somethign to do with my JS.

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String currentTypeTarifIdStr = request.getParameter("current_typetarif_id");
        Integer currentTypeTarifId = Integer.valueOf(currentTypeTarifIdStr);
        String inputName = request.getParameter("inputName");
        String inputCode = request.getParameter("inputCode");
        String inputTarif = request.getParameter("inputTarif");
        BigDecimal amount = new BigDecimal(inputTarif);
        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(",");
//        for (String optionSplitStr : optionSplitStrs) {
//            String trimmedOptionSplitStr = optionSplitStr.trim();
//            // do something with trimmedOptionSplitStr
//            trimmedOptionSplit = Integer.valueOf(trimmedOptionSplitStr);
//        }

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

        if (!Objects.equals(currentTypeTarifId, null) || !Objects.equals(TypeTarifIdToRemove, null) || !Objects.equals(TypeTarifOptionToRemove, null)) {
            try {
                mUserTransaction.begin();
                if ((Integer.valueOf(TypeTarifIdToRemove) != 0) && (Integer.valueOf(TypeTarifOptionToRemove) != 0)) {// Delete Mode
//                    Query query = mEntityManager.createQuery("DELETE FROM TarifTypeOptionEntity WHERE be_type_tarif_id=:pId AND be_tarif_option_id=:pId")
//                                .setParameter("pId", TypeTarifOptionToRemove);
                    Query query = mEntityManager.createQuery("DELETE FROM TarifTypeEntity WHERE id=:pId")
                                .setParameter("pId", TypeTarifIdToRemove);
                    int resultCount = query.executeUpdate();
                    processRequest(request, response);
                    mUserTransaction.commit();
                    return;
                } 

                if (currentTypeTarifId == 0) { // Add mode
                    TarifTypeEntity typeTarifEntity = new TarifTypeEntity();
                    typeTarifEntity.setLibelle(inputName);
                    typeTarifEntity.setCode(inputCode);
                    typeTarifEntity.setMontantTarifDefaut(amount);
                    mEntityManager.persist(typeTarifEntity);

                    TarifTypeOptionEntity typeTarifTypeOption = new TarifTypeOptionEntity();
                    typeTarifTypeOption.setTypeTarifId(currentTypeTarifId);
                    typeTarifTypeOption.setTarifOptionId(trimmedOptionSplit);
                    mEntityManager.persist(typeTarifTypeOption);
                }
                else { // Modification mode
                    Query query = mEntityManager.createQuery("FROM TarifTypeEntity WHERE id=:pId")
                            .setParameter("pId", currentTypeTarifId);
                    List<TarifTypeEntity> typeTarifEntityList = query.getResultList();
                    if (!typeTarifEntityList.isEmpty()) {
                        TarifTypeEntity typeTarifEntity = typeTarifEntityList.get(0);
                        typeTarifEntity.setLibelle(inputName);
                        typeTarifEntity.setCode(inputCode);
                        typeTarifEntity.setMontantTarifDefaut(amount);
                        mEntityManager.persist(typeTarifEntity);



                        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();
                    }
                }
                mUserTransaction.commit();
            }
            catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException | SecurityException | IllegalStateException ex) {
                Logger.getLogger(UserManagementServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        processRequest(request, response);
    }

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.




Aucun commentaire:

Enregistrer un commentaire