【发布时间】:2018-12-21 22:20:43
【问题描述】:
我正在尝试使用 java 脚本对我的表进行计算,但我不知道如何将它与其余的 id 循环。现在我只能计算单个项目如何使这个倍数这里是结果
正如你所见,只有 id 1 得到结果,而 id 2 为 null 我怎样才能使这项工作在这里是我的 java 脚本
function calc(){
var n1 = parseFloat(document.getElementById('n1').value);
var n2 = parseFloat(document.getElementById('n2').value);
var oper = document.getElementById('result').value = n1*4+n2;
}
<table id="my-table" class="table table-hover table-bordered">
<thead>
<tr >
<th class="text">NAME</th>
<th class="text">A/P</th>
<th class="text">H/W</th>
<th class="text">Result</th>
</tr>
</thead>
<tbody>
@foreach($scores as $index => $score)
<tr>
<td>{{$score->lead->student_name}} <input type="hidden" name="scores[{{$loop->index}}][id]" value="{{$score->id}}"></td>
<td style="text-align:center"><input id="n1" type="text" name="scores[{{$loop->index}}][jan_ap]" value="{{$score->jan_ap}}" class="input" autocomplete="off"></td>
<td style="text-align:center"><input id="n2" type="text" name="scores[{{$loop->index}}][jan_hm]" value="{{$score->jan_hm}}" class="input" autocomplete="off"></td>
<td style="text-align:center"><input id="result" type="text" name="scores[{{$loop->index}}][result]" value="{{$score->result}}" class="input" autocomplete="off"></td>
</tr>
@endforeach
</tbody>
</table><div class="form-group ">
<button onclick="calc(); " type="submit">Submit</button>
</div>
【问题讨论】:
-
你能改写你的问题吗?您提到了表格,但您向我们展示了一个带有 json 的 api。我应该假设您使用 json 使用刀片创建表吗?阅读的好主意:stackoverflow.com/help/how-to-ask
-
对不起,请尝试再次阅读我的问题,我向您展示的图像是我从表中返回的结果,我试图计算表中的每个 id..
-
@Grace 当您说表格时,您是指 HTML 表格,还是您的数据库表格?您提到此 javascript 在刀片模板中。如果是这样,您可能以错误的方式处理此问题。您似乎正在尝试从某个 DOM 元素中读取值,但您可能更容易将数据直接传递给刀片模板中的 javascript,请参阅此处的文档:laravel.com/docs/5.6/blade#displaying-data 这也是示例:` `
-
计算后要显示在结果文本框中?
-
如果可能的话..
标签: javascript php laravel for-loop