javascript - 提交时无法查看显示总和的字段值

标签 javascript php jquery

我有一个动态字段,在其中输入数值并求和以在总计字段中给出总计。代码如下:

Click here to view the code

HTML

<form method="post" action="questup.php">
  <div class="panel panel-primary">
    <div class="bs-example">
      <div class="Container">
        <table class="table table-hover small-text" style="margin-top: 50px;" id="tb" border="1px">
          <tbody>

          </tbody>
        </table>
      </div>
    </div>
  </div>
  <Footer Align="Center">
    <button type="submit" class="btn btn-primary" id="SubmitButton">Submit</button>
  </footer>
</form>

JS

$(function() {
      var students = 0;

      function studentTemplate(students) {

        return '<tr class="student_marks"><td>Marks</td><td> <input type="number" name="marks[]" class="marks student_' + students + ' form-control" student_ref="student_' + students + '" placeholder="Marks"> </td><th><a style="font-size:18px;width:33%;" class="addsubject" ><span class="glyphicon glyphicon-plus"></span></a></th></tr><tr class="subtotal"> <td><b>Grand Total</b></td><td> <input type="text" name="subtotals[]" class="row-total student_' + students + ' form-control" disabled> </td></tr>';
      }

      function subTemplate(students) {
        return '<tr class="student_marks"><td>Marks</td> <td> <input type="number" name="marks[]" class="marks student_' + students + ' form-control" student_ref="student_' + students + '" placeholder="Marks"> </td><th><a style="font-size:18px;width:33%;" class="addsubject" ><span class="glyphicon glyphicon-plus"></span></a></th> </tr>';
      }


      $('#tb tbody').append(studentTemplate(students));

      $('#tb tbody').on("click", "a.addsubject", function() {
        $(this).closest(".student_marks").after(subTemplate(students));
      });

      $('#tb tbody').on("keyup", "input.marks", function() {
        subtotal($(this).attr("student_ref"));
      });

      function subtotal(ref) {
        var sub = 0;
        $(".marks." + ref).each(function() {
          sub += $(this).val() != "" ? parseInt($(this).val()) : 0;
        });
        $(".row-total." + ref).val(sub);
        calcTotal();
      }

    });

Issue

当我提交表单并使用

调用总计字段时
$_POST['subtotals'] 

在下一页上,它不返回任何值。

enter image description here

我尝试使用 var_dump($_POST['subtotals']) 调试数据,但它返回 Null

PHP Code

<?PHP
 echo var_dump($_POST['subtotals'])."<br/>";
 echo var_dump($_POST['marks'])."<br/>";

for($i=0;$i<count($_POST['marks']);$i++)
{

    echo "Marks : ".$_POST['marks'][$i]."<br/>";    
}

echo "Grand Total: ".$_POST['subtotals']."<br/>";

?>

如何将自动求和字段值显示到下一页?我不需要现成的代码,但希望得到建议! :)

最佳答案

用这个函数替换你的函数:

function studentTemplate(students) {

        return '<tr class="student_marks"><td>Marks</td><td> <input type="number" name="marks[]" class="marks student_' + students + ' form-control" student_ref="student_' + students + '" placeholder="Marks"> </td><th><a style="font-size:18px;width:33%;" class="addsubject" ><span class="glyphicon glyphicon-plus"></span></a></th></tr><tr class="subtotal"> <td><b>Grand Total</b></td><td> <input type="text" name="subtotals[]" class="row-total student_' + students + ' form-control" > </td></tr>';
      }

我已删除“小计”字段的“禁用”属性。禁用的输入无法提交。

关于javascript - 提交时无法查看显示总和的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35573989/

相关文章:

jquery - jquery隐藏鼠标光标的方法

jquery - 为什么 jQuery 不总是加载?

jQuery 移动 : Listview Filter Search Callback Function

javascript - 如何防止 jQuery 插件更改我的 DOM?

javascript - 正则表达式以匹配以点开头和结尾的字符串

php - 多CRUD系统Laravel 5.2

java - 如何使用与 PHP 一起使用的 Java HttpClient 库上传文件

javascript - 从一系列 promise 中获取值

javascript - 与js中的__proto_和prototype混淆

php - SQL - 按子字段重新分组