使用jQuery的表数据总和和平均值

时间:2018-10-18 10:43:00

标签: php jquery html codeigniter

我一直在研究这段代码,这使我可以在模态窗口中设置学生成绩,并且我一直在尝试使用jQuery计算总体总分,但是每次尝试它都会给我(0)作为答案肯定是错误的。

部分学生名单

Students List

用于设置结果的模态窗口 Modal Window

我的密码

控制器:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

class Subject_scores extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('main_model');
        $this->load->model('scores_model');
    }

    public function index()
    {
    $term = $this->input->post('term');
    $sess = $this->input->post('session');
    $level = $this->input->post('level');
    $reg_id = $this->input->post('reg_id');
    $query = $this->scores_model->get_scores($level.'_subject_scores', '*', array(
            'reg_id'  => $reg_id,
            'session' => $sess,
            'term'    => $term
        ));

        $output = '';

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $key) {
                $output .='<tr>
                                <td data-id1="'.$key->id.'" contenteditable>'.$key->subject.'</td>
                                <td data-id2="'.$key->id.'" contenteditable>'.$key->ca1.'</td>
                                <td data-id3="'.$key->id.'" contenteditable>'.$key->ca2.'</td>
                                <td data-id4="'.$key->id.'" contenteditable>'.$key->ca3.'</td>
                                <td data-id5="'.$key->id.'" contenteditable>'.$key->project.'</td>
                                <td data-id6="'.$key->id.'" contenteditable>'.$key->exam.'</td>
                                <td id="total_calc" data-id7="'.$key->id.'" contenteditable>'.$key->total.'</td>
                                <td data-id8="'.$key->id.'" contenteditable>'.$key->grade.'</td>
                                <td><a class="w3-btn w3-btn-floating w3-red delete" id="'.$key->id.'">-</a></td>
                            </tr>';

            }

        $output .= '<tr>
                                <td id="subject" contenteditable></td>
                                <td id="ca1" class="data" contenteditable></td>
                                <td id="ca2" class="data" contenteditable></td>
                                <td id="ca3" class="data" contenteditable></td>
                                <td id="project" class="data" contenteditable></td>
                                <td id="exam" class="data" contenteditable></td>
                                <td id="total"></td>
                                <td id="grade"></td>
                                <td><a class="w3-btn w3-btn-floating w3-teal add">+</a></td>
                            </tr>';

        } else {
            $output .= '<tr>
                            <td id="subject" contenteditable></td>
                            <td id="ca1" class="data" contenteditable></td>
                            <td id="ca2" class="data" contenteditable></td>
                            <td id="ca3" class="data" contenteditable></td>
                            <td id="project" class="data" contenteditable></td>
                            <td id="exam" class="data" contenteditable></td>
                            <td id="total"></td>
                            <td id="grade"></td>
                            <td><a class="w3-btn w3-btn-floating w3-teal add">+</a></td>
                        </tr>';
        }
    echo $output;
    }
}
    ?>

查看

<table align="center" id="newTable" class="table table-bordered table-striped"> 
  <tr>
    <td>Name</td>
    <td colspan="3" id="sname">Some  Name</td>
    <td>Term</td>
    <input type="hidden" name="term" value="<?php echo $term ?>">
    <input type="hidden" name="session" value="<?php echo $session ?>">
    <input type="hidden" name="level" value="">
    <input type="hidden" name="reg_id" value="">
    <input type="hidden" name="class" value="">
    <input type="hidden" name="department" value="">
    <td><?php switch ($term) {
      case '1st':
        echo "FIRST TERM";
        break;

        case '2nd':
        echo "SECOND TERM";
        break;

      case '3rd':
        echo "THIRD TERM";
        break;
    } ?></td>
    <td>session</td>
    <td><?php echo $session ?></td>
  </tr>
  <tr>
    <td>Class</td>
    <td id="class">SSS 1</td>
    <td>Total</td>
    <td id="overall_total">300</td>
    <td>average</td>
    <td id="average">30.0</td>
    <td>next term begins</td>
    <td><?php echo $this->main_model->get_specific_data('term_closure', 'res_date', array('session' => $session, 'term' => $term), 'res_date'); ?></td>
  </tr>
</table>

<table align="center" class="table table-striped table-bordered small">
  <thead>
    <tr>
    <th>Subjects</th>
    <th>Res. Test</th>
    <th>Mid-Term Test</th>
    <th>CA</th>
    <th>Project</th>
    <th>Exam</th>
    <th>Total</th>
    <th>Grade</th>
  </tr>
  </thead>
  <tbody id="body2">


  </tbody>

</table>

javascript

   function overall_total_cal() {
  var sum = 0;
  $('[id="total_calc"]').each(function() {
    var value = Number($(this).text());
    sum += parseFloat(value);
  });
  $('[id="overall_total"]').text(sum);
}

0 个答案:

没有答案