试图获取jQuery中每一行的总成本
Trying to get total costs in each row jQuery
嗨,我正试图从每一行中获得总成本。下面是一张截图:
链接:https://i.stack.imgur.com/4ZgLt.png
正如你所看到的,一行已经通过乘积5*2计算出了它的总数。但这只发生在第一行,但我很乐意在每一行中进行计算。我认为这是因为每行中的名称变量相同,这就是为什么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>
有人能帮我吗?
其次,您可能希望通过获取事件的当前行并查找其中要更改的元素,将计算限制在同一行。
第三,你不需要分离你的keyup函数,这只会让事情变得更容易。事实上,由于第二个keyup函数是updateTotal的副本,因此它完全没有必要:
// Added a variable that this function accepts, "row", so it knows its location in the document
var updateTotal = function(row){
// Define all elements as descendants of the "row" passed in
var salary = row.find(".hour_salary"),
hour = row.find(".hour"),
total = row.find(".total_salary");
var salaryVal = parseFloat(salary.val());
var hourVal = parseFloat(hour.val());
// Added another check for hourVal being a number as well
if (!isNaN(salaryVal) || !isNaN(hourVal)) {
total.val(salaryVal * hourVal);
} else {
total.val("not a number?");
}
};
//Run same keyup function for both text boxes
$(".hour_salary, .hour").keyup(function() {
// Find the row
var row = $(this).closest('tr');
// Pass in the row
updateTotal(row);
});
注意:我已经将所有的ID选择器都更改为类,所以在HTML中反映出来之前,这将不起作用。
希望这能有所帮助。
有点晚了,但我已经用以下代码修复了这个问题(对于感兴趣的人):
@foreach ($project->projecttask as $pt)
<tr>
<td>
{{ Form::select('id_task', array('0' => 'no task selected', 'tasks'=>Task::lists('task_name','id') ), null, array('class' => 'form-control')) }}
</td>
<td>
{{$pt->hour}}
<input type="hidden" name="projectId" value="{{$project->id}}"/>
</td>
<td>
{{ Form::text('hour', $pt['hour'], array('class' => 'form-control hour', 'data-row'=>$pt->id , 'id'=>'hour' . $pt->id )) }}
</td>
<td>
{{ Form::text('hour_salary', $pt->hour_salary, array('class' => 'form-control hour_salary', 'data-row'=>$pt->id , 'id'=>'hour_salary' . $pt->id )) }}
</td>
<td>
{{ Form::text('total_salary', $pt->total_salary, array('class' => 'form-control', 'id'=>'total_salary' . $pt->id, '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>
{{ $pt->id }}
<ul class="dropdown-menu" aria-labelledby="selDelete" role="menu">
<li><a href= "{{ route('user.projecttasks.destroy',array( 'id' => $pt->id, 'project' => $project->id )) }}" data-method="delete" >Delete</a></li>
</ul>
</div>
</td>
</tr>
@endforeach
<script>
$(function(){
//$('#test').css('color','red');
updateTotal();
});
var updateTotal = function(rowId){
var rowId = rowId,
$hour_salary = $("#hour_salary" + rowId),
$total_salary = $("#total_salary" + rowId);
var hour = parseFloat($("#hour" + rowId).val()),
hour_salary = parseFloat($hour_salary.val());
if ( hour_salary.length > 0 )
$total_salary.val("0");
else
$total_salary.val( hour_salary * hour );
};
$(".hour_salary").keyup(function() {
var $this = $(this);
//wrm row ipv data-row?
var rowId = $this.data('row');
$this.val( $this.val().replace(/'D/g,'') );
updateTotal( rowId );
});
$(".hour").keyup(function() {
var $this = $(this);
var rowId = $this.data('row');
$this.val( $this.val().replace(/'D/g,'') );
updateTotal( rowId );
});
</script>
老实说,我得到了实习主管的帮助,制定了这个代码。
相关文章:
- 拆分文本以每隔n个字符添加一行新行,并注意空格
- 动态插入的表:JQuery未检测到最后一行
- 使用JavaScript动态插入DIV的成本有多高
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 如何在Angular UI网格中选择下一行
- 通过单击动态加载的表中同一行的另一个字段来更新一行的字段
- 我的购物应用程序不会将用户想要购买的东西相加,也不会打印总成本
- 无法获取表单总成本
- 挖空:无法将数量变化与购物车中的总成本绑定
- 在挖空购物车中创建总成本函数
- Javascript/JQuery 显示单个席位的总成本
- 检查可变总成本的最小值,如果小于最小值,则显示警报消息
- 试图获取jQuery中每一行的总成本
- 未捕获引用错误:未定义总成本
- javascript中的总成本函数
- 如果我选择上一行日期不更新,并且当单击删除时,顶部总结果值没有减少,则JQuery克隆单击添加更多按钮
- 如何根据从不同选项卡中选择的项目添加总成本
- 复选框将输入添加到文本框总选择和总选择成本
- 计算选定项目的成本,然后计算总成本
- 基于复选框单选按钮调整总成本标价