嗨我试图从每一行获得总成本。下面是截图怎么说实际上看起来:试图获得每行总成本jQuery
链接:http://i.imgur.com/0lY4sJl.png
正如你可以看到已经拥有了总通过乘法5 * 2计算的1排。但是这只发生在第一行,但我很乐意在每行中计算它。我认为它是因为每行中的同名变量,这就是为什么javascript不能计算每行的总数。我想我需要一个for循环来解决这个问题。但我真的不知道如何。这里是我目前的代码:
@foreach ($project->projecttask as $pt)
<tr>
<td>
{{ $pt->task['task_name'] }}
</td>
<td>
<input type="hidden" name="projectId" value="{{$project->id}}"/>
{{Form::hidden('hour', $project->hour)}}
</td>
<td>
{{ Form::text('hour', $pt->hour, array('class' => 'form-control', 'id'=>'hour')) }}
</td>
<td>
{{ Form::text('hour_salary', $pt->hour_salary, array('class' => 'form-control', 'id'=>'hour_salary')) }}
</td>
<td>
{{ Form::text('total_salary', $pt->total_salary, array('class' => 'form-control', 'id'=>'total_salary', 'disabled')) }}
{{-- {{ Form::text('total_salary', $project->total_salary, array('class' => 'form-control', 'id'=>'total_salary', 'disabled')) }}--}}
</td>
<td>
<div class="dropdown">
<button type="button" class="btn btn-danger dropdown-toggle selDelete" data-toggle="dropdown">
<input id="check1" name="checkbox[]" type="checkbox" class="check" >
<span class="caret-hover caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="selDelete" role="menu">
<li><a href= "{{ route('user.projecttasks.destroy',array($pt->id)) }}" data-method="delete" >Delete</a></li>
</ul>
</div>
</td>
</tr>
@endforeach
<script>
$(function(){
updateTotal();
});
var updateTotal = function(){
var hour_salary = parseFloat($("#hour_salary").val()); // get number as float
// alternately parseInt(string, 10), in case you work with integers
var hour = parseFloat($("#hour").val());
if (!isNaN(hour_salary)) { // the input is a number
$("#total_salary").val(hour_salary * hour); // update second field
} else { // the input wasn't a number
$("#total_salary").val("not a number?"); // show an error mesage
}
};
// we used jQuery 'keyup' to trigger the computation as the user type
$("#hour_salary").keyup(function() { // when key is released in "#inputfield1"
// "change()" is also possible instead of "keyup()", slightly different behavior
updateTotal();
});
// we used jQuery 'keyup' to trigger the computation as the user type
$("#hour").keyup(function() { // when key is released in "#inputfield1"
// "change()" is also possible instead of "keyup()", slightly different behavior
var hour_salary = parseFloat($("#hour_salary").val()); // get number as float
// alternately parseInt(string, 10), in case you work with integers
var hour = parseFloat($("#hour").val());
if (!isNaN(hour_salary)) { // the input is a number
$("#total_salary").val(hour_salary * hour); // update second field
} else { // the input wasn't a number
$("#total_salary").val("not a number?"); // show an error mesage
}
});
</script>
有人可以帮我吗?
看起来像你有多个元素具有相同的ID ...元素的ID必须是唯一的....使用类来分组类似的元素 – 2014-11-06 09:16:04
您可以发布生成的HTML,而不是模板/脚本? – 2014-11-06 09:16:06
我没有看到任何jQuery或JavaScript。这些标签有关吗? – 2014-11-06 09:19:14