简体   繁体   English

jQuery-单击表行以在复选框中附加值

[英]jquery - click on table row to append value in checkboxes

JS JS

 $(document).ready(function() { $('.tg tr.data-list-row[name=data-list-row]').click(function() { var currentId = $(this).closest('tr').attr('id'); $.getJSON('<%=request.getContextPath()%>/ws/booking/getpriceinput_by_id/' + currentId, function(data) { $("#current_typetarif_id").val(data.id); $("#inputName").val(data.libelle); $("#inputCode").val(data.code); $("#inputTarif").val(data.montantTarifDefaut); }); }); }); 
 <div class="table-responsive"> <table class="table tg" style="table-layout: fixed; width: 745px"> <colgroup> <col style="width: 249px"> <col style="width: 249px"> <col style="width: 249px"> </colgroup> <thead> <tr> <th class="tg-s6z2 bdleft">NOM</th> <th class="tg-s6z2">CODE</th> <th class="tg-s6z2">PRIX PAR DEFAUT</th> </tr> </thead> <tfoot> </tfoot> <tbody> <tr> <td colspan="5"> <div class="scrollit"> <table class="table tg" style="table-layout: fixed;"> <colgroup> <col style="width: 220px"> <col style="width: 240px"> <col style="width: 240px"> </colgroup> <c:forEach items="${type_tarif_list}" var="type_tarif" varStatus="loop"> <tr class="data-list-row" name="data-list-row" id="${type_tarif.id}" style="cursor: pointer;"> <td class="tg-s6z2 bdleft">${type_tarif.libelle}</td> <td class="tg-s6z2">${type_tarif.code}</td> <td class="tg-s6z2">${type_tarif.montantTarifDefaut}</td> <td class="deleterow bdryt" id="${type_tarif.id}" name="del_button"> <div class="glyphicon glyphicon-remove" title="Supprimer"></div> </td> </tr> </c:forEach> </table> </div> </td> </tr> </tbody> </table> </div> <form class="form-horizontal" style="margin: 0 auto; width: 150px;" id="scrollit" name="thisForm"> <c:forEach items="${option_tarif_list}" var="option_tarif" varStatus="loop"> <div class="checkbox1"> <label> <input type="checkbox" name="tarif_inclue" value="${option_tarif.libelle}" class="checkboxchk" id="option_tarif_chk_${option_tarif.id}">${option_tarif.libelle} </label> </div> </c:forEach> </form> 

JSON value extracted from database 从数据库提取的JSON值

{
  "id": 1,
  "code": "0001",
  "libelle": "TARIF PUBLIC",
  "montantTarifDefaut": 10.00,
  "tarifTypeList": [
    {
      "chambreTypeId": 1,
      "tarifTypeId": 1
    }
  ],
  "tarifTypeOptionList": [
    {
      "typeTarifId": 1,
      "tarifOptionId": 1
    },
    {
      "typeTarifId": 1,
      "tarifOptionId": 2
    },
    {
      "typeTarifId": 1,
      "tarifOptionId": 3
    }
  ]
}

Hi. 你好

This block of code below make a select in the table row to display values into the texts fields. 下面的这段代码在表行中进行选择,以将值显示在文本字段中。

    $(document).ready(function() {

  $('.tg tr.data-list-row[name=data-list-row]').click(function() {
    var currentId = $(this).closest('tr').attr('id');
    $.getJSON('<%=request.getContextPath()%>/ws/booking/getpriceinput_by_id/' + currentId, function(data) {
      $("#current_typetarif_id").val(data.id);
      $("#inputName").val(data.libelle);
      $("#inputCode").val(data.code);
      $("#inputTarif").val(data.montantTarifDefaut);
    });
  });



});

On clicking on the table row, i need to display the checked values in the checkbox. 在单击表行时,我需要在复选框中显示选中的值。 According to the id selected in the rows, specific values will be checked in the checkboxes. 根据在行中选择的ID,将在复选框中检查特定的值。 Those values are being extracted from a database through a JSON file. 这些值是通过JSON文件从数据库中提取的。 I should extract it using the value ( data.tarifOptionId ) 我应该使用值( data.tarifOptionId )提取它

I think is should be put it in a loop, so that the id value is incremented when each table row is clicked. 我认为应该将其放在循环中,以便在单击每个表行时增加id值。 The id of the input is #option_tarif_chk_ , and it is embeded in a cforeach loop. 输入的ID为#option_tarif_chk_ ,并将其嵌入cforeach循环中。

I have written something like this below: 我在下面写了这样的东西:

            $.each(data, function(i, item) {
                alert(item.tarifTypeOptionList[0].tarifOptionId);
            });

But it is not working. 但这是行不通的。 The alert returns Uncaught TypeError: Cannot read property '0' of undefined 警报返回Uncaught TypeError: Cannot read property '0' of undefined

Any help will be much appreciated. 任何帮助都感激不尽。

Thank you 谢谢

Ok i managed to do it with the following code: 好的,我设法用以下代码做到了:

$('input[type="checkbox"]').each(function() {
                        $("#option_tarif_chk_" + data.tarifTypeOptionList[0].tarifOptionId).prop('checked', true);

                });

But now the issue is, how to get the other values from the JSON, i had to initialise it to 0 tarifTypeOptionList[0] , which in turns return only the first value 但是现在的问题是,如何从JSON获取其他值,我不得不将其初始化为0 tarifTypeOptionList[0] ,而后者仅返回第一个值

"tarifTypeOptionList": [
    {
      "typeTarifId": 1,
      "tarifOptionId": 1
    },

.

Note: i have used the .prop() method to make the selection in the checkbox. 注意:我已经使用.prop()方法在复选框中进行选择。

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

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