2016-08-23 69 views
1

我正在做的是我有一個下拉式的下拉式變化,我的第一個ajax正在運行。 Onchange ajax獲取所有服務及其成本。首先阿賈克斯運行良好。現在我有一個複選框,用戶可以選擇多個複選框之前的所有服務。打開檢查複選框我的secong ajax正在運行。我想要的是檢查複選框我想要所有選中的複選框的總和,但它顯示價格不是所有選中的總和。請看代碼。通過ajax在codeigniter中顯示所有選中的複選框的總和

我的AJAX是:

<script> 
    $(document).ready(function(){ 
     $('#serv_pack').change(function(){ 
     $.ajax({ 
       type: "POST", 
       url: "<?php echo base_url(); ?>admin/pages/create_package", 
       data: { 'serv_package' : $('#serv_pack').val() }, 
       success: function(data) 
       { 
       $('#ser_pac').html(data).addClass("md-card"); 
       }, 
       }); 
     }); 
     $(document).on('click','.check_price',function() { 
     $.ajax ({ 
       type: "POST", 
       url: "<?php echo base_url(); ?>admin/pages/check_price", 
       data: { 'checked_price[]' : $(this).val() }, 
       success: function(data) 
       { 
        $('#ser_pac1').html(data).addClass("md-card"); 
       }, 
      }); 
     }); 
    }); 
    </script> 

我的HTML是:

<div class="uk-grid" data-uk-grid-margin=""> 
    <div class="uk-width-large-1-2 uk-width-medium-1-2"> 
     <label for="service_title">Select Service Type<span class= 
     "req">*</span></label> <select data-md-selectize="" id="serv_pack" name= 
     "service_type" required=""> 
     <option value=""> 
      Service Types 
     </option><?php foreach ($ser_type->result() as $catt){?> 
     <option value="<?php echo $catt->tenure_name; ?>"> 
      <?php echo $catt->tenure_name; ?> 
     </option><?php } ?> 
     </select> 
    </div> 
    </div> 
    <div class="md-card-content"> 
    <table cellspacing="0" class="uk-table" id="ser_pac" width="100%"> 
    </table><span id="ser_pac1"></span> 
    </div> 

和我的控制器是

public function create_package() { 
    $service_type = $this->input->post('serv_package'); 
    $this->load->model('home/Home_model'); 
    $types = $this->Home_model->get_service_type($service_type); 
    $theader = ''; 
    $theader. = "<th> Service Title </th>"; 
    $theader. = "<th> Price </th>"; 
    $tbody = "<tbody>"; 
    foreach($types->result() as $typo) { 
     $se_ty = $typo->service_type_cost; 
     $se_ty1 = $typo->service_cost; 
     $se_arr1 = explode(",", $se_ty); 
     $ser_ar = explode(",", $se_ty1); 
     for ($i = 0; $i < count($se_arr1); $i++) { 
      if ($se_arr1[$i] == $service_type) {@ 
       $price = $ser_ar[$i]; 
       $service_title = $typo->service_title; 
       $service_id = $typo->id; 
       $tbody. = "<tr>"; 
       $tbody. = "<td> <input type=checkbox value='$price' class='check_price' id='".$service_id. 
       "'> ".$service_title. 
       "</td>"; 
       $tbody. = "<td> $ &nbsp".$price. 
       "</td>"; 
       $tbody. = "</tr>"; 
      } 
     } 
     echo '<input type="hidden" value="'.$service_title. 
     '" name="ser_tit[]">'; 
    } 
    $tbody. = "<tr>"; 
    $tbody. = "</tr>"; 
    $tbody. = "</tbody>"; 
    $table = '<table>'.$theader.$tbody. 
    '<br></table>'; 
    echo $table; 
} 

public function check_price() { 
    $prr = $this->input->post('checked_price'); 
    $text = $this->input->post('checked_val'); 
    foreach($prr as $pr1) { 
     $sum = 0; 
     $sum += $pr1; 
     echo '<span><b>Total Cost</b></span>'; 
    } 
    echo $sum; 
} 

請幫我做這件事......我真的很迷茫在這。問題第二阿賈克斯

回答

1

您發送當前選中的複選框才值,而不是找到所有的檢查項目和它的值發送

$(document).on('click', '.check_price', function() { 
    var checked = $('.check_price:checked').map(function() { 
    return this.value; 
    }).get(); 
    $.ajax({ 
    type: "POST", 
    url: "<?php echo base_url(); ?>admin/pages/check_price", 
    data: { 
     'checked_price[]': checked 
    }, 
    success: function(data) { 
     $('#ser_pac1').html(data).addClass("md-card"); 
    }, 
    }); 
}); 
+0

謝謝你真的作品...但阿倫P佐尼我有還有一個問題,我可以問嗎? – Pardeep

+0

@Pardeep它是什麼? –

+0

我如何取數組中的所有複選框的名稱。您可以在create_package控制器中看到輸入類型複選框,並且其名稱位於$ service_title變量中。 – Pardeep

相關問題