【问题标题】:if check box is checked then get product name and price in a array如果选中复选框,则在数组中获取产品名称和价格
【发布时间】:2021-01-25 09:42:25
【问题描述】:

如果选中复选框,我想在数组中获取服务名称及其价格。enter image description here 图片也如下所示: 获取选定项目的值(即服务名称和数组中的价格)。请告诉我如何获得。

$(document).on('click', '.completed_request', function(){
            var completed_request_id = $(this).attr('request_id');
            var prescription = $(this).parent().parent().find('td').eq(2).text();
            console.log((prescription));
            var arr_val = prescription.split(',');
            if(arr_val != null){
                var lihtml =  "";
                arr_val.forEach(val => {
                  //lihtml+='<li>'+val+'</li>';
                  lihtml+='<li><div class="form-check form-check-inline">\n'+
                      '<input class="form-check-input inlineCheckbox1 checkedId"  type="checkbox" value="option1">\n'+
                    '<label class="form-check-label add" for="inlineCheckbox1" id="">'+val+'</label>\n'+
                  '</div>';
                  lihtml+='<input type="text" class="checkedId cal_total checked" id="file_id">\n</li>';
                });
               // console.log(lihtml);
                $("#val_html").html(lihtml);

            }
            bootbox.confirm("Do you want to complete this request?", function (result) {
                if (result == true) {
                    $('#prescription_names').text(prescription);
                    $('#completedModalId #request_id').val(completed_request_id);
                    $('#completedModalId').modal('show');
                     $('#total_amount').val('');
                }

            });
        });
/*
* to selected item names and price
*/
 $(document).on("click", ".checkedId", function() {
            if ($('.checkedId').prop('checked') == true) {
             //console.log($(this).next('label').text());
             var selectedItemsNames = $(this).next('label').text();
             //var row = $('.cal_total').closestval();
             //$('.cal_total').addClass("checked").removeClass("unchecked");
             var checks = [];
             var ss = $('.cal_total').each(function(index, item){
                checks.push( item );

             });
             console.log(row);
             console.log(selectedItemsNames);
            }
        });

【问题讨论】:

  • html代码在哪里?
  • 在 jquery 中我调用了 html
  • 您需要在json-array 或简单数组中获取不同数组中的两个值?
  • 两个值的简单数组

标签: javascript html jquery jquery-ui laravel-5


【解决方案1】:

当用户点击复选框时,您可以使用each 循环遍历所有checked 复选框并向数组添加新值,即:names , prices

演示代码

$(document).on("click", ".checkedId", function() {
  var prices = [];
  var names = [];
  //loop through lis with checked checkboxes
  $("ul li ").find(".checkedId:checked").each(function(index, item) {
    //get value of price
    prices.push($(this).closest('li').find(".cal_total").val());
    //get value of names and push in array
    names.push($(this).closest('li').find('label').text());
  });

  console.log(prices)
  console.log(names)


});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>
    <div class="form-check form-check-inline">
      <input class="form-check-input inlineCheckbox1 checkedId" type="checkbox" value="option1">
      <label class="form-check-label add" for="inlineCheckbox1" id="">something</label>
    </div>
    <input type="text" class="checkedId cal_total checked" id="file_id" value="25"></li>
  <li>
    <div class="form-check form-check-inline">
      <input class="form-check-input inlineCheckbox1 checkedId" type="checkbox" value="option1">
      <label class="form-check-label add" for="inlineCheckbox1" id="">something2</label>
    </div>
    <input type="text" class="checkedId cal_total checked" id="file_id" value="27"></li>
</ul>

【讨论】:

  • 但是我没有得到最后一个输入值
  • 哪个输入?请详细说明?
  • 好吧,妈妈,当我点击复选框时,我得到了这个 ["Amylase"] [""] 即获取服务名称但价格变空。单击第二个复选框 ["Amylase"," 仅限 ASOT "] ["1", ""] 等等。
  • 在你的html代码中价格是cal_total这个输入?根据您的代码,目前您的文本框也是空的,因此首先您需要为其添加一些值,然后选中复选框并查看是否有效。另外,请确保我在演示代码中创建的 html 是相同的,否则您可以创建代码的 jsfiddle 会很好。
猜你喜欢
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多